How to get / process POST data in ASP.NET MVC 4 Razor?

I had a problem getting / processing POST data from <form> in an ASP.NET MVC 4 Razor project.

What I've done?

Do not discuss my code, I am only testing ASP.NET MVC 4.

As you can see in the controller code, I also made a Model for user information:

 public class AuthForm { public string Login { get; set; } public string Password { get; set; } } 

I thought that ASP.NET, if the model is correct, will automatically analyze the data in the model, but it is wrong.

Then I tried using .Request["name"] , but (the inputs were not empty):

enter image description here

I also tried using the following Attributes :

  • [HttpPost]
  • [AcceptVerbs(HttpVerbs.Post)]

But there is no success!

What did I misunderstand? Please help me fix my problems.

thanks

+4
source share
1 answer

You need to use helper methods so that MVC knows how to bind values, and then in the controller you can use the model (since you will understand the model’s connecting device)

eg.

 @model Models.AuthForm @{ ViewBag.Title = ""; } @section home { @using (Html.BeginForm("Auth", "Controller")) { <div class="control-group"> @Html.LabelFor(model => model.Login, new { @class = "control-label" }) <div class="controls"> @Html.TextBoxFor(model => model.Login, new { @class = "input-large", autocapitalize = "off" }) @Html.ValidationMessageFor(model => model.Login, "*", new { @class = "help-inline" }) </div> </div> <div class="control-group"> @Html.LabelFor(model => model.Password, new { @class = "control-label" }) <div class="controls"> @Html.PasswordFor(model => model.Password, new { @class= "input-large" }) @Html.ValidationMessageFor(model => model.Password, "*", new { @class = "help-inline" }) </div> </div> <div class="form-actions"> <input type="submit" class="btn btn-primary" value="Log On" /> </div> } } 

Using your own HTML controls is an option, but you will need to do this in the same way as the helper methods above, otherwise the model will not be filled.

+2
source

Source: https://habr.com/ru/post/1487802/


All Articles