Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get post value in .NET Core

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.

like image 658
Millenial2020 Avatar asked Aug 16 '17 15:08

Millenial2020


People also ask

How can we call post method from controller in .NET core?

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.

What is HTTP post in ASP NET?

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.

What is get and post method in MVC?

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 ...


2 Answers

Several ways:

  • Add a FirstName and LastName (case insensitive) string parameter to the Create method that accepts a post
  • Add a parameter of type IFormCollection to this same method and access the named values FirstName and LastName
  • Directly access the values from HttpContext.Request.Form
  • Access the values from the ModelState property
  • Add a model parameter of a POCO class that holds whatever values you want to submit
like image 158
Ricardo Peres Avatar answered Sep 29 '22 19:09

Ricardo Peres


Add 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.

like image 30
Shyju Avatar answered Sep 29 '22 19:09

Shyju