Passing data from a form without HTML helpers

Can I transfer data to the controller Without using Html.EditorFor , etc.

Just using simple HTML inputs such as:

View:

 @using (Html.BeginForm("Add", "Parameter", FormMethod.Post, new { enctype = "multipart/form-data" })) { <fieldset> <input type="text" name="product.Name" id="product.Name"/> <input type="text" name="product.Description" id="product.Description"/> <input type="submit"> </fieldset> } 

Controller:

 [HttpPost] public ActionResult Add(Product product) { return null; } 
+4
source share
1 answer

Yes you can, but you need to configure the input names with the names of the ViewModel properties:

 <input type="text" name="product.Name" id="product.Name"/> <input type="text" name="product.Description" id="product.Description"/> 

Must be:

 <input type="text" name="Name" id="name"/> <input type="text" name="Description" id="description"/> 

You should not add the ViewModel type as a prefix for input names.

+1
source

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


All Articles