How to interact with the <t> list in MVC

I have the following code in my opinion

    <% foreach (var item in Model.stats)
       {%> 
       <label style="style="float:left;"><%= item.Stat_Caption %></label>
       <%=Html.TextBox(item.Stat_Caption,item.Stat_Value) %>

       <%} %>

I am trying to turn a stats object, which is just a list, into a list of text fields so that the user can update them.

with whom I worked, how can I apply values ​​back to the list collection after updating the text fields by the user?

+3
source share
1 answer

You need to wrap the text fields in the form:

<% using (Html.BeginForm()) { %>
    <% foreach (var item in Model.stats)
       {%> 
       <label style="style="float:left;"><%= item.Stat_Caption %></label>
       <%=Html.TextBox(item.Stat_Caption,item.Stat_Value) %>

       <%} %>

    <input type="submit" value="Save" class="button" /></td>
<% } %>

When you press the submit button, it will perform a standard POST with key / value pairs, for example:

Box1 : Hello
Box2 : World

On the controller side, you need a method that receives a POST request:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Entity entity)
{
    // code goes here
}

Entity - . MVC , , , :

public class Entity()
{
    public string Box1 { get; set; }
    public string Box2 { get; set; }
}

Box1 Box2 , POST.

, :

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
    // code goes here
}

collection - . , Object, , .

+4

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


All Articles