I am completely new to ASP.NET Core(2 weeks in) and I need to learn Web dev all over again with ASP.net for work and yeah, trying to figure it all out. so forgive the probably noob question.
I am having issues passing data from HTML form to a Model and then to the Controller that I can't figure out. Here is my issue.
Here is basic HTML:
<form method="post" action="/FormProcWithModels" role="form">
<input asp-for="UserName" type="text" id="UserName" placeholder="Enter your Full Name" />
<input type="submit" />
</form>
Here is Model:
namespace project001.Models
{
public class ContactPageModel
{
public string UserName{ get; set; }
}
}
Here is the Controller:
Edit to show more code. This is my GET method
[HttpGet]
public IActionResult Contact()
{
ViewBag.PageTitle = "This is the Contact page";
return View();
}
[HttpPost("FormProcWithModels")]
public IActionResult Contact(ContactPageModel model)
{
return Content($"The form username entered is {model.UserName}");
}
So as an example, when I enter the name of "Jim" into the form and submit it, the page loads with "The form username entered is:" but the name doesn't pass through.
I don't get errors or anything and I'm not good enough to figure out why the data is null.
Thanks in advance for any assistance given.
Edit:
When I do it like this:
[HttpPost("FormProcWithoutModels")]
public IActionResult Contact(string uName)
{
string currentUser = uName;
ViewBag.PageTitle = "This is the Contact page";
//return View();
return Content($"The form username entered is {currentUser}");
}
It works without Model. Soon as I try with Models, it doesn't work!