Asp.net mvc 如何与列表交互<;t>;在MVC中
我的视图中有以下代码Asp.net mvc 如何与列表交互<;t>;在MVC中,asp.net-mvc,Asp.net Mvc,我的视图中有以下代码 <% foreach (var item in Model.stats) {%> <label style="style="float:left;"><%= item.Stat_Caption %></label> <%=Html.TextBox(item.Stat_Caption,item.Stat_Value) %> <%} %>
<% foreach (var item in Model.stats)
{%>
<label style="style="float:left;"><%= item.Stat_Caption %></label>
<%=Html.TextBox(item.Stat_Caption,item.Stat_Value) %>
<%} %>
您需要将文本框包装成以下形式:
<% 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>
<% } %>
在控制器端,您需要有一个接收POST请求的方法:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Entity entity)
{
// code goes here
}
其中,实体
是您的数据模型对象。默认的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
是对象字典。使用此字典的坏处在于它是一个对象类型的字典,因此您必须获取数据并将其转换回它应该是的任何类型
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
// code goes here
}