I have a Customer controller that has these 2 action methods.
[HttpGet]
public IActionResult Create()
{
ViewBag.Title = "Customers";
return View();
}
[HttpPost]
public IActionResult Create(IFormCollection form)
{
return View();
}
I also created a Customer view.
<form class="form-horizontal" method="post" action="/Customer/Create">
<div class="form-group">
<label class="col-lg-2 control-label">FirstName *</label>
<div class="col-sm-6">
<input type="text" class="form-control" required name="FirstName">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">LastName *</label>
<div class="col-sm-6">
<input type="text" class="form-control" required name="LastName">
</div>
</div>
</form>
How do I get the post values? Every time I submit the form it goes to the [HttpGet]
and I want to use the [HttpPost]
. I'm new to ASP.NET Core so sorry for the simple question.
You can use some built in . net core tag helpers to specify what method you want to call once the form gets submitted. Make sure your button has the type="submit" . That tells the form that whenever it's clicked, go ahead and make a post request to that handler.
HttpGet and HttpPost, both are the method of posting client data or form data to the server. HTTP is a HyperText Transfer Protocol that is designed to send and receive data between client and server using web pages. HTTP has two methods that are used for posting data from web pages to the server.
Both GET and POST method is used to transfer data from client to server in HTTP protocol but the Main difference between the POST and GET method is that GET carries request parameter appended in URL string while POST carries request parameter in message body which makes it more secure way of transferring data from ...
Several ways:
FirstName
and LastName
(case insensitive) string parameter to the Create
method that accepts a postIFormCollection
to this same method and access the named values FirstName
and LastName
HttpContext.Request.Form
ModelState
propertyAdd 2 parameter to your http post action method which is matching with your form element names.
[HttpPost]
public IActionResult Create(string firstName,string lastName)
{
return View();
}
Another option is to create view model class with properties matching to form element names. This is useful when you have many input elements in the form and do not want to add clutter to the parameter list of your action method.
public class LoginVm
{
public string FirstName { set;get;}
public string LastName { set;get;}
}
and
[HttpPost]
public IActionResult Create(LoginVm model)
{
// to do : user model.FirstName and model.LastName
return View();
}
When the form is submitted, the model binder will read the posted form data and map it to the view model object property values.
Remember, view model is specific to the view. So if your view(form) is sending 5 fields, create a simple POCO view model with only those 5 fields.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With