I have a Register form which uses the Compare Attribute to compare the password and confirm password. When I enter in different passwords it does what it is supposed to and says "Passwords do not match". The issue comes when I hit the submit button and "Post" it. I hit a breakpoint at "if (!ModelState.IsValid)" where ModelState is not valid. When it reloads the page where it used to say "Passwords do not match" it now says "Could not find a property named Password." I tried doing what this post said to try and put in the answer's code. All it returns is that the error is "null".
Razor Page:
#page
#model ThinBlueLie.Pages.RegisterModel
#{
ViewBag.Title = "Register";
}
<div class="container-fluid h-100 row nogap">
<div class="card border-secondary mx-auto center col-lg-3 col-md-4 p-0" style="margin-top:100px;">
<div class="card-header">
Register
<a class="float-right" asp-page="/Login">Login</a>
</div>
<form method="post">
<div class="card-body text-secondary">
<div class="form-group">
<label asp-for="Users.Email" class="control-label">Email</label>
<input type="email" asp-for="Users.Email" class="form-control">
<span asp-validation-for="Users.Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password"class="control-label"></label>
<input asp-for="Password" type="password" class="form-control" >
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ConfirmPassword" class="control-label"></label>
<input type="password" asp-for="ConfirmPassword" class="form-control">
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Users.Username" class="control-label"></label>
<input type="text" asp-for="Users.Username" class="form-control">
<span asp-validation-for="Users.Username" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Register" class="btn btn-primary mb-1" style="float:right" />
</div>
</div>
</form>
<div class="card-footer">
<a>Log in with Google</a>
</div>
</div>
</div>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
PageModel:
namespace ThinBlueLie.Pages
{
public class RegisterModel : PageModel
{
private readonly DataAccessLibrary.thinblue.ThinbluelieContext _context;
public RegisterModel(DataAccessLibrary.thinblue.ThinbluelieContext context)
{
_context = context;
}
public IActionResult OnGet()
{
return Page();
}
public Users Users { get; set; }
[BindProperty]
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[BindProperty]
[DataType(DataType.Password)]
[Display(Name = "Confirm Password")]
[Compare("Password", ErrorMessage = "Passwords do not match")]
public string ConfirmPassword { get; set; }
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see https://aka.ms/RazorPagesCRUD.
public async Task<IActionResult> OnPostAsync()
{
var errors = ModelState.Values.SelectMany(v => v.Errors);
if (!ModelState.IsValid)
{
return Page();
}
_context.Users.Add(Users);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
}
Model:
namespace DataAccessLibrary.thinblue
{
public partial class Users
{
public int IdUsers { get; set; }
[BindProperty]
public string Username { get; set; }
[BindProperty]
[Required]
[EmailAddress]
public string Email { get; set; }
public string Password { get; set; }
}
}
I reproduce your error,and I put Password and ConfirmPassword in the same model,and it works.Here is a demo:
PassWordModel:
public class PassWordModel
{
[BindProperty]
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[BindProperty]
[DataType(DataType.Password)]
[Display(Name = "Confirm Password")]
[Compare("Password", ErrorMessage = "Passwords do not match")]
public string ConfirmPassword { get; set; }
}
cshtml.cs:
public class RegisterModel : PageModel
{
public IActionResult OnGet()
{
return Page();
}
public Users Users { get; set; }
[BindProperty]
public PassWordModel passWordModel { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
return RedirectToPage("./Index");
}
}
cshtml:
<div class="container-fluid h-100 row nogap">
<div class="card border-secondary mx-auto center col-lg-3 col-md-4 p-0" style="margin-top:100px;">
<div class="card-header">
Register
<a class="float-right" asp-page="/Login">Login</a>
</div>
<form method="post">
<div class="card-body text-secondary">
<div class="form-group">
<label asp-for="Users.Email" class="control-label">Email</label>
<input type="email" asp-for="Users.Email" class="form-control">
<span asp-validation-for="Users.Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="passWordModel.Password" class="control-label"></label>
<input asp-for="passWordModel.Password" type="password" class="form-control">
<span asp-validation-for="passWordModel.Password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="passWordModel.ConfirmPassword" class="control-label"></label>
<input type="password" asp-for="passWordModel.ConfirmPassword" class="form-control">
<span asp-validation-for="passWordModel.ConfirmPassword" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Users.Username" class="control-label"></label>
<input type="text" asp-for="Users.Username" class="form-control">
<span asp-validation-for="Users.Username" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Register" class="btn btn-primary mb-1" style="float:right" />
</div>
</div>
</form>
<div class="card-footer">
<a>Log in with Google</a>
</div>
</div>
</div>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
result:
Related
I have two models, User and Employee. I have to make a registration form, where I'll be taking inputs for both these models. I know how to do this with a single model but I cannot figure out how to do it for multiple models. I have read some suggestions here where they've suggested using a ViewModel but I cannot configure it properly.
User:
public class UserModel
{
public int Id { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
Employee:
public class EmployeeModel
{
public int Id { get; set; }
public string Name { get; set; }
public int Phone { get; set; }
public int UserId { get; set; }
public int LocationId { get; set; }
}
ViewModel:
public class UserEmployee
{
public UserModel user { get; set; }
public EmployeeModel emp { get; set; }
}
My controllers:
[HttpGet]
public ActionResult RegisterEmployee()
{
var model = new UserEmployee();
model.user = new UserModel();
model.emp = new EmployeeModel();
return View(model);
}
[HttpPost]
public ActionResult RegisterEmployee(UserEmployee useremp)
{
return View();
}
Form:
#model ZeroHungerProject.Models.UserEmployee
///////
<form action="" method="post">
<div class="form-group">
<label>User Name</label>
<input type="text" name="#Model.user.UserName" class="form-control" placeholder="User Name">
</div>
<div class="form-group">
<label>Email address</label>
<input type="email" name="#Model.user.Email" class="form-control" placeholder="Email">
</div>
<div class="form-group">
<label>Phone</label>
<input type="number" name="#Model.emp.Phone" class="form-control" placeholder="Phone">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="#Model.user.Password" class="form-control" placeholder="Password">
</div>
<div class="form-group">
<label>Confirm Password</label>
<input type="password" class="form-control" placeholder="Confirm Password">
</div>
<button type="submit" class="btn btn-primary btn-flat m-b-30 m-t-30">Register</button>
I tried using the viewmodel to post the inputs. But no data is passing from the form.
Remove the prefix "#Model." from the name attribute.
<form action="" method="post">
<div class="form-group">
<label>User Name</label>
<input type="text" name="user.UserName" class="form-control" placeholder="User Name">
</div>
<div class="form-group">
<label>Email address</label>
<input type="email" name="user.Email" class="form-control" placeholder="Email">
</div>
<div class="form-group">
<label>Phone</label>
<input type="number" name="emp.Phone" class="form-control" placeholder="Phone">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="user.Password" class="form-control" placeholder="Password">
</div>
<div class="form-group">
<label>Confirm Password</label>
<input type="password" class="form-control" placeholder="Confirm Password">
</div>
<button type="submit" class="btn btn-primary btn-flat m-b-30 m-t-30">Register</button>
</form>
And to pass ConfirmPassword, you need to provide the name attribute to the <input> element for ConfirmPassword and add the ConfirmPassword property in the UserModel class.
<input type="password" name="user.ConfirmPassword" class="form-control" placeholder="Confirm Password">
public class UserModel
{
...
public string ConfirmPassword { get; set; }
}
I have a problem, where I have a form, and some of the data is not being set in the model and I don't know why or where to look.
My Model:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace VolunteerMngSystm.Models
{
public class Organisations
{
public int ID { get; set; }
[Required]
public string Organisation_name { get; set; }
[Required]
public string Industry { get; set; }
[Required]
public string Email { get; set; }
[Required]
public string OrganisationsID { get; set; }
}
}
The Email and OrganisationID are not being set.
My Controller Action method:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> OrgReg([Bind("Organisation_name,Industry,Email,OrganisationID")] Organisations organisations)
{
try
{
if (ModelState.IsValid)
{
_context.Add(organisations);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(OrgHome));
}
}
catch (DbUpdateException e)
{
//Log the error (uncomment ex variable name and write a log.
ModelState.AddModelError("" + e, "Unable to save changes. " +
"Try again, and if the problem persists " +
"see your system administrator.");
}
return View(organisations);
}
My View:
#model VolunteerMngSystm.Models.Organisations;
#{
ViewData["Title"] = "Register";
}
<h1>Register</h1>
<h5>As Organisation</h5>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="OrgReg">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Organisation_name" class="control-label">Organisation Name</label>
<input asp-for="Organisation_name" class="form-control" />
<span asp-validation-for="Organisation_name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Industry" class="control-label"></label>
<input asp-for="Industry" class="form-control" />
<span asp-validation-for="Industry" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Email" class="control-label"></label>
<input asp-for="Email" class="form-control" />
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="OrganisationsID" class="control-label">Verification ID</label>
<input asp-for="OrganisationsID" type="file" accept="image/* " />
<span asp-validation-for="OrganisationsID" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Register" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Can anyone please advise me on where to look or how to fix this problem, please?
Since your action need AntiForgeryToken, add it to your form
<form asp-action="OrgReg">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
#Html.AntiForgeryToken()
or remove it from the action
also try to remove [Bind] from the action. You have a typo there and you don't need it since you are binding all properties any way.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> OrgReg( Organisations organisations)
I am trying to validate specific fields from my model before the form is submitted. It works but message doesn't show, this is because name attributes doesn't match.
This is my model:
public class TaskList
{
[Key]
public int Id { get; set; }
[MaxLength(200, ErrorMessage = "Subject field can't have more than 250 characters.")]
[Required(ErrorMessage = "Subject field cannot be empty.")]
[Column(TypeName = "nvarchar")]
public string Subject { get; set; }
[MaxLength(1000, ErrorMessage = "Details field can't have more than 1000 characters.")]
[Column(TypeName = "nvarchar")]
public string Details { get; set; }
[Required(ErrorMessage = "Please select a module.")]
public TaskLabels Label { get; set; }
[Required]
[Column(TypeName = "date")]
public DateTime Created { get; set; }
[Column(TypeName = "date")]
public DateTime? DueDate { get; set; }
[Column(TypeName = "date")]
public DateTime? Completed { get; set; }
}
This is controller action:
[HttpPost]
public IActionResult TaskList(TaskList newTask)
{
_ticketRepo.AddTask(newTask);
return RedirectToAction("TaskList");
}
This is my viewmodel:
public class TaskListViewModel
{
public TaskList ToDo { get; set; }
public IEnumerable<TaskList> TasksList { get; set; }
}
And this is my form:
<div class="modal fade" id="modal-add">
<div class="modal-dialog modal-lg">
<form class="form-horizontal" asp-controller="home" asp-action="TaskList" method="post">
<div class="modal-content">
<div class="modal-header bg-success">
<h4 class="modal-title">Add New To Do Item</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="card-body">
<div class="form-group row">
<label asp-for="ToDo.Subject" class="col-sm-2 col-form-label">Subject</label>
<div class="col-sm-10">
<input type="text" asp-for="ToDo.Subject" name="Subject" class="form-control" placeholder="Subject">
<span asp-validation-for="#Model.ToDo.Subject" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="ToDo.Details" class="col-sm-2 col-form-label">Details</label>
<div class="col-sm-10">
<textarea asp-for="ToDo.Details" name="Details" class="form-control" placeholder="Provide some details about the task."></textarea>
<span asp-validation-for="ToDo.Details" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="ToDo.Label" class="col-sm-2 col-form-label">Module</label>
<div class="col-sm-10">
<select name="ToDo.Label" asp-for="Label" asp-items="Html.GetEnumSelectList<TaskLabels>()" class="form-control select2 select2-danger" data-dropdown-css-class="select2-danger" style="width: 100%;">
<option value="" selected="selected">Please select one</option>
</select>
<span asp-validation-for="ToDo.Label" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label asp-for="ToDo.DueDate" class="col-sm-2 col-form-label">Date:</label>
<div class="col-sm-10 input-group date" id="duedate" data-target-input="nearest">
<input type="text" name="DueDate" asp-for="ToDo.DueDate" class="form-control datetimepicker-input" data-target="#duedate" />
<div class="input-group-append" data-target="#duedate" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
<div asp-validation-summary="All" class="text-danger"></div>
</div>
<!-- /.card-body -->
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</div>
<!-- /.modal-content -->
</form>
</div>
<!-- /.modal-dialog -->
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script><script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
In my form, if I remove name attribute from input fields, validation works, validation messages are shown but model binding doesn't work and I get null exception error for my properties. What am I doing wrong?
Of course I had to instantiate viewmodel in my controller action! When I did that and removed all name attributes from input fields in my view, everything worked just fine!
[HttpPost]
public IActionResult TaskList(TaskListViewModel model)
{
if (ModelState.IsValid)
{
TaskList taskList = new TaskList
{
Created = DateTime.Now.Date,
Subject = model.ToDo.Subject,
Details = model.ToDo.Details,
DueDate = model.ToDo.DueDate,
Label = model.ToDo.Label
};
_ticketRepo.AddTask(taskList);
return RedirectToAction("TaskList");
}
else
{
return View();
}
}
My HttpPost Edit task is not giving me my list of EventMembers. I put a watch on the GET for my Edit task and it reads my EventMembers just fine. but when i get my POST Edit my EventMembers only shows System.Collections.Generic.List`1[System.String] in my watch window, as well as my input box in my view. Whats happening?
Model:
public class Event
{
[Required]
public int EventId { get; set; }
[ForeignKey("UserId")]
public virtual SchedulerUser SchedulerUser { get; set; }
[MaxLength(50)]
public string EventCreator { get; set; }
public List<string> EventMembers { get; set; }
[Required]
[MaxLength(100)]
public string Subject { get; set; }
[MaxLength(400)]
public string Description { get; set; }
[Required]
public DateTime StartTime { get; set; }
public DateTime? EndTime { get; set; }
[Required]
public bool IsFullDay { get; set; }
[Required]
public bool AcceptOrDecline { get; set; }
}
Controller:
// GET: Events/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var #event = await _context.Events.FindAsync(id);
if (#event == null)
{
return NotFound();
}
return View(#event);
}
// POST: Events/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("EventId,UserId,EventCreator,EventMembers,Subject,Description,StartTime,EndTime,IsFullDay,AcceptOrDecline")] Event #event)
{
if (id != #event.EventId)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
#event.SchedulerUser = await _userManager.GetUserAsync(HttpContext.User);
_context.Update(#event);
if (#event.AcceptOrDecline == false)
{
_context.Remove(#event);
}
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!EventExists(#event.EventId))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(#event);
}
View:
#model Scheduler.Models.Event
#{
ViewData["Title"] = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Edit</h1>
<h4>Event</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="EventId" />
<div class="form-group">
<label asp-for="Subject" class="control-label"></label>
<input asp-for="Subject" class="form-control" />
<span asp-validation-for="Subject" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Description" class="control-label"></label>
<input asp-for="Description" class="form-control" />
<span asp-validation-for="Description" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EventMembers" class="control-label"></label>
<input asp-for="EventMembers" class="form-control" />
<span asp-validation-for="EventMembers" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="StartTime" class="control-label">Start Time</label>
<input asp-for="StartTime" class="form-control" />
<span asp-validation-for="StartTime" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EndTime" class="control-label">End Time</label>
<input asp-for="EndTime" class="form-control" />
<span asp-validation-for="EndTime" class="text-danger"></span>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="IsFullDay" /> All Day
</label>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="AcceptOrDecline" /> Accepted
</label>
<p style="color:red; font-weight:bold;">Uncheck this to decline invitation. Event will be removed from your schedule.</p>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Since your EventMembers model property is an Enumerable list, the Html control which represents the property should be indexed.
<div class="form-group">
<label asp-for="EventMembers[0]" class="control-label"></label>
<input asp-for="EventMembers[0]" class="form-control" />
<span asp-validation-for="EventMembers[0]" class="text-danger"></span>
</div>
<div class="form-group">
...
<input asp-for="EventMembers[1]" class="form-control" />
...
</div>
The best practice is to generate the control inside a loop.
#for (int i = 0; i < Model.EventMembers.Count; i++)
{
<div class="form-group">
...
<input asp-for="EventMembers[i]" class="form-control" />
...
</div>
}
I'm trying to add a "Create" to one of my Controllers (LeagueController), so I created a Create View, which uses my League object.
However, when I go to submit the form, it does not redirect back to the Index view, as I want it to, nor does it enter the log entry to say that I created a league.
League Class
public class League : BaseEntity
{
[Required]
[DataType(DataType.Text)]
public string LeagueName { get; set; }
[Required]
[DataType(DataType.Text)]
public string LeagueInitials { get; set; }
[DataType(DataType.Text)]
public string LeagueURL { get; set; }
[DataType(DataType.DateTime)]
public DateTime Founded { get; set; }
[InverseProperty("League")]
public ICollection<Team> Teams { get; set; }
[ForeignKey("LeagueID")]
public ICollection<LeagueOwners> LeagueOwners { get; set; }
}
LeaguesController Class
public class LeaguesController : Controller
{
private MyDBContext context;
private ILogger logger;
public LeaguesController(MyDBContext context, ILogger logger)
{
this.context = context;
this.logger = logger;
}
public IActionResult Index()
{
this.logger.LogInformation("Reached League Index");
return View();
}
[Route("Create")]
public IActionResult Create()
{
this.logger.LogInformation("Creating a league");
return View();
}
[HttpPost]
public IActionResult Create(League league)
{
this.logger.LogInformation("Create button clicked!");
return this.RedirectToAction("Index");
}
}
Create.cshtml
#model MySite.Core.Entities.League
#{
ViewData["Title"] = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
<h4>League</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="LeagueName" class="control-label"></label>
<input asp-for="LeagueName" class="form-control" />
<span asp-validation-for="LeagueName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="LeagueInitials" class="control-label"></label>
<input asp-for="LeagueInitials" class="form-control" />
<span asp-validation-for="LeagueInitials" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="LeagueURL" class="control-label"></label>
<input asp-for="LeagueURL" class="form-control" />
<span asp-validation-for="LeagueURL" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Founded" class="control-label"></label>
<input asp-for="Founded" class="form-control" />
<span asp-validation-for="Founded" class="text-danger"></span>
</div>
<div class="form-group" hidden="hidden">
<label asp-for="Created" class="control-label"></label>
<input asp-for="Created" class="form-control" value="#DateTime.Now" />
<span asp-validation-for="Created" class="text-danger"></span>
</div>
<div class="form-group" hidden="hidden">
<label asp-for="Modified" class="control-label"></label>
<input asp-for="Modified" class="form-control" value="#DateTime.Now" />
<span asp-validation-for="Modified" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
The first way is that you could delete [Route("Create")]which is on your get method.
The second way is that you could add [Route] attribute to your post method like below:
[Route("Create")]
public IActionResult Create()
{
this.logger.LogInformation("Creating a league");
return View();
}
[Route("Create")]
[HttpPost]
public IActionResult Create(League league)
{
this.logger.LogInformation("Create button clicked!");
return this.RedirectToAction("Index");
}
In your form <form asp-action="Create" method="post"> add a tag to point to the correct controller asp-controller="Leagues"
Do not enforce the route, that's just anti-pattern.