C# 为什么我能';t将此模型传递给ASP.NET MVC中的控制器
为什么我不能将此模型传递给ASP.NET MVC中的控制器 我的看法是:C# 为什么我能';t将此模型传递给ASP.NET MVC中的控制器,c#,asp.net-mvc,C#,Asp.net Mvc,为什么我不能将此模型传递给ASP.NET MVC中的控制器 我的看法是: @model Pro.WebUI.ViewModels.DataItemVm <div> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.FirstSetList.FirstOrDefault().Na
@model Pro.WebUI.ViewModels.DataItemVm
<div>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstSetList.FirstOrDefault().Name)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstSetList.FirstOrDefault().Amount)
</th>
</tr>
@foreach (var item in Model.FirstSetList)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.TextBoxFor(modelItem => item.Amount)
</td>
</tr>
}
</table>
<hr/>
<div>
@using (Html.BeginForm())
{
@Html.HiddenFor(model => model.FirstSetList)
<input type="submit" value="Confirm"/>
}
</div>
</div>
当然,我的ViewModel DataItemVm有以下列表:
public class DataItemVm
{
public List<FirstSetViewModel> FirstSetList { get; set; }
}
公共类DataItemVm
{
公共列表FirstSetList{get;set;}
}
所以看起来这个问题只在眼前。也许你应该在别的地方?但是,HiddenFor应该发送足够的数据来发送此模型。将@foreach cycle更改为for-ASP.NET MVC使用索引器为元素生成正确的ID和名称,因此您应该使用@Html.TextBoxFor(x=>x.Array[index])为集合正确生成Html 本周期:
@foreach (var item in Model.FirstSetList)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.TextBoxFor(modelItem => item.Amount)
</td>
</tr>
}
@foreach(Model.FirstSetList中的变量项)
{
@DisplayFor(modelItem=>item.Name)
@Html.TextBoxFor(modelItem=>item.Amount)
}
应该是这样的:
@for (var i = 0; i < Model.FirstSetList.Length; i++)
{
<tr>
<td>
@Html.DisplayFor(model => model.FirstSetList[i].Name)
@Html.HiddenFor(model => model.FirstSetList[i].Name) //otherwise name won't be returned to controller
</td>
<td>
@Html.TextBoxFor(model=> model.FirstSetList[i].Amount)
</td>
</tr>
}
(变量i=0;i@中
查看这篇关于ASP.NET MVC中模型绑定的文章:
将@foreach cycle更改为for-ASP.NET MVC使用索引器为元素生成正确的ID和名称,因此您应该使用@Html.TextBoxFor(x=>x.Array[index])为集合正确生成Html
本周期:
@foreach (var item in Model.FirstSetList)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.TextBoxFor(modelItem => item.Amount)
</td>
</tr>
}
@foreach(Model.FirstSetList中的变量项)
{
@DisplayFor(modelItem=>item.Name)
@Html.TextBoxFor(modelItem=>item.Amount)
}
应该是这样的:
@for (var i = 0; i < Model.FirstSetList.Length; i++)
{
<tr>
<td>
@Html.DisplayFor(model => model.FirstSetList[i].Name)
@Html.HiddenFor(model => model.FirstSetList[i].Name) //otherwise name won't be returned to controller
</td>
<td>
@Html.TextBoxFor(model=> model.FirstSetList[i].Amount)
</td>
</tr>
}
(变量i=0;imodel.FirstSetList[i].Name)
@Html.HiddenFor(model=>model.FirstSetList[i].Name)//否则名称将不会返回给控制器
@Html.TextBoxFor(model=>model.FirstSetList[i].Amount)
}
另一个问题是-要发送到控制器的数据的标记应使用(Html.BeginForm()){//put it here}块放入@中
查看这篇关于ASP.NET MVC中模型绑定的文章:
当我的模型是元组时,我也遇到了同样的问题。使用FormCollection作为post方法的参数解决了这个问题生成了什么样的HTML?我猜HiddenFor(model=>model.FirstSetList)
不知道如何在单个隐藏输入中实际表示FirstSetViewModel
列表。它不是简单地序列化它或其他东西。您可能需要为其创建一个特定的编辑模板。您必须使用EditorFor
来呈现集合当我的模型是元组时,我也会遇到同样的问题。使用FormCollection作为post方法的参数解决了这个问题生成了什么样的HTML?我猜HiddenFor(model=>model.FirstSetList)
不知道如何在单个隐藏输入中实际表示FirstSetViewModel
列表。它不是简单地序列化它或其他东西。您可能需要为其创建特定的编辑模板。您必须使用EditorFor
来呈现集合谢谢!顺便说一句,你忘了把它围起来。如果可以,请更正它-也许有一天会有帮助。@DiPix抱歉,我忽略了那个按钮,更新了帖子。你知道为什么按钮显示在表格上方而不是表格下方吗?@DiPix你可能已经在
标签中加了。如果没有,请在当前浏览器中使用开发者控制台检查布局。谢谢!顺便说一句,你忘了把它围起来。如果可以,请更正它-也许有一天会有帮助。@DiPix抱歉,我忽略了那个按钮,更新了帖子。你知道为什么按钮显示在表格上方而不是表格下方吗?@DiPix你可能已经在
标签中加了。如果没有,请在当前浏览器中使用开发者控制台检查布局。