I had a problem with an elementary post sending request. While I googled, even more confused. Either the extension .aspx, then cshtml. My brain is already boiling.

I have a users model. This model contains the following properties: Name and LastName

class Users { public string Name{ get; set; } public string LastName { get; set } } 

Also in the UsersController controller, there is a RegisterUser method, which takes an object of the Users class as a parameter.

  public ActionResult RegisterUser(Users user) { //какие-то действия з БД и т.д. return View("Registration completed"); } 

There is also a form:

 <form method="post" action=""> <input type="text" name="Name"> <input type="text" name="LastName"> <input type="Submit">Register</input> </form> 

I don't know what to write to the action property so that the RegisterUser function is called.

1 answer 1

Usually web forms on asp.net mvc do not write in html-code, but use helpers.

In your case, the form will look like this:

 @model Users @using (Html.BeginForm("RegisterUser", "Users", new { }, FormMethod.Post, new { @class = "" })) { @Html.TextBoxFor(x => x.Name) @Html.TextBoxFor(x => x.LastName) <button type="submit">Register</button> } 

Read the documentation about BeginForm parameters (actionName / controllerName / etc) and select the most appropriate overload.

However, if you really want plain html, you can use @ Url.Action:

 <form method="post" action="@Url.Action("RegisterUser", "Users")"> <input type="text" name="Name"> <input type="text" name="LastName"> <input type="Submit">Register</input> </form> 
  • And why is it better to use helpers, and not html code? \ - Castiel_Luciefer2000
  • one
    @ Castiel_Luciefer2000 This 'mvc way' gives ready-made typing of fields (autocomplete when writing, convenience when refactoring). But this is just another method proposed by Microsoft, they later abandoned it in the asp.net core - there the approach changed again, making a roll back towards hetemeralization, while preserving the happiness of typing. - AK