如何将列表/数组从视图返回到控制器
我的 ViewResult 控制器:
public ViewResult Controller(int id)
{
List<Data> dataList = dataAccess.getDataById(id);
Results[] resultArray = new Results[dataList.Count];
ViewBag.results= resultArray;
return View(dataList);
}
我的视图:
@model IEnumerable<Solution.Models.Data>
@{
Solution.Models.Results[] res= ViewBag.results;
}
@using (Html.BeginForm()) {
<table>
@{
int i = 0;
foreach (var item in Model) {
<tr>
<td>
Snippet: @Html.DisplayFor(modelItem => item.text)
</td>
</tr>
<tr>
<td>
Translation: @Html.EditorFor(modelItem => res[i].text)
</td>
</tr>
i++;
}
}
</table>
<p>
<input class="CRUD-buttons" type="submit" value="Send" />
</p>
}
我的控制器 ActionResult:
[HttpPost]
public ActionResult Controller(/*List<Data> dataList, */Results[] results)
{
ResultText = results[0].text; //NullReferenceException
}
dataList 和结果为空。我在 stackoverflow 上读了几篇文章,但找不到解决方案。
我已经看过以下博客(链接),但是它的 MVC 2 代码。 :(
My ViewResult Controller:
public ViewResult Controller(int id)
{
List<Data> dataList = dataAccess.getDataById(id);
Results[] resultArray = new Results[dataList.Count];
ViewBag.results= resultArray;
return View(dataList);
}
My View:
@model IEnumerable<Solution.Models.Data>
@{
Solution.Models.Results[] res= ViewBag.results;
}
@using (Html.BeginForm()) {
<table>
@{
int i = 0;
foreach (var item in Model) {
<tr>
<td>
Snippet: @Html.DisplayFor(modelItem => item.text)
</td>
</tr>
<tr>
<td>
Translation: @Html.EditorFor(modelItem => res[i].text)
</td>
</tr>
i++;
}
}
</table>
<p>
<input class="CRUD-buttons" type="submit" value="Send" />
</p>
}
My Controller ActionResult:
[HttpPost]
public ActionResult Controller(/*List<Data> dataList, */Results[] results)
{
ResultText = results[0].text; //NullReferenceException
}
dataList and results are empty. I read a couple of posts on stackoverflow, but could not find a solution.
I took already a look on the following blog (link) but its MVC 2 code. :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有多种方法可以做到这一点。这里的意思是,为了让您接收
results
参数,生成的编辑控件的name
应该是results[0]
对于第一个,results[1]
对于第二个,等等,索引中没有间隙(这是因为 在表单发布时,DefaultModelBinder
期望如何找到名为 的字段) 。因此,一个直接的(尽管不是很好)解决方案是正确指定名称:
更好的解决方案是将
结果
放入您的模型中(更好的是,在专门为此视图创建的 ViewModel)。例如,首先创建一个类来封装一条数据和相应的结果:然后让视图将这些项的集合作为其模型:
然后输出编辑控件
最后,修改回发操作接受
ItemViewModel
数组:一般建议: 尽量避免将
EditorFor
与不属于视图模型一部分的对象一起使用(例如例如,你经历过的事情ViewBag
或ViewData
)。如果你这样做,MVC 就会惩罚你。There are multiple ways to do this. What gets you here is that in order for you to receive the
results
parameter, thename
for the generated edit controls should beresults[0]
for the first,results[1]
for the second, etc with no gaps in the indexes (this is because of howDefaultModelBinder
expects to find the fields named when the form is posted).So one immediate (although not very good) solution would be to specify the name correctly:
A much better solution would be to put the
results
into your model (better yet, in a ViewModel created specifically for this view). For example, first of all you create a class that encapsulates one piece of data and the corresponding result:You then make your view have a collection of these items as its model:
and then output the edit control with
Finally, you modify the postback action to accept an array of
ItemViewModel
:General piece of advice: Try to avoid using
EditorFor
with objects that are not part of your view's model (for example, things you have passed throughViewBag
orViewData
). If you do this, MVC will punish you.