Reputation: 6318
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!
Upvotes: 2
Views: 6090
Reputation: 678
I’ve found that in ASP.Net Core, model binding isn’t always as automatic. Try:
[HttpPost("FormProcWithModels")]
public IActionResult Contact([FromForm] ContactPageModel model)
{
return Content($"The form username entered is {model.UserName}");
}
Upvotes: 0
Reputation: 4298
You can also get submitted value using HttpContext.Request
params
[HttpPost("FormProcWithModels")]
public IActionResult Contact()
{
var UserName = HttpContext.Request.Form["UserName"]
return Content($"The form username entered is {UserName}");
}
using FormCollection
[HttpPost("FormProcWithModels")]
public IActionResult Contact(FormCollection Fc)
{
var UserName = Fc["UserName"].ToString();
return Content($"The form username entered is {UserName}");
}
Upvotes: 2
Reputation: 7866
I assume that this is because, you don't have get method. You need to add get method to take user input.
So, basically your controller looks like:
[HttpGet]
public IActionResult Contact()
{
return View();
}
[HttpPost("FormProcWithModels")]
public IActionResult Contact(ContactPageModel model)
{
return Content($"The form username entered is {model.UserName}");
}
View page:
@model ContactPageModel
<form method="post" action="/FormProcWithModels">
<input asp-for="UserName" type="text" id="UserName" placeholder="Enter your Full Name" />
<input type="submit" />
</form>
Though it's not working, add role="form"
<form method="post"... role="form">
...
...
</form>
Upvotes: 2