如何将列表/数组从视图返回到控制器

发布于 2024-12-19 18:58:22 字数 1390 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫儰鉨ミ守葔 2024-12-26 18:58:22

有多种方法可以做到这一点。这里的意思是,为了让您接收 results 参数,生成的编辑控件的 name 应该是 results[0]对于第一个,results[1] 对于第二个,等等,索引中没有间隙(这是因为 在表单发布时,DefaultModelBinder 期望如何找到名为 的字段) 。

因此,一个直接的(尽管不是很好)解决方案是正确指定名称:

@Html.TextBox(string.Format("results[{0}]", i), res[i].text)

更好的解决方案是将结果放入您的模型中(更好的是,在专门为此视图创建的 ViewModel)。例如,首先创建一个类来封装一条数据和相应的结果:

class ItemViewModel
{
    Solution.Models.Data TheData { get; set; }
    Solution.Models.Results TheResults { get; set; }
}

然后让视图将这些项的集合作为其模型:

@model IEnumerable<ItemViewModel>

然后输出编辑控件

Translation: @Html.EditorFor(modelItem => modelItem.TheResults)

最后,修改回发操作接受 ItemViewModel 数组:

[HttpPost] 
public ActionResult Controller(ItemViewModel[] results) 

一般建议: 尽量避免将 EditorFor 与不属于视图模型一部分的对象一起使用(例如例如,你经历过的事情ViewBagViewData)。如果你这样做,MVC 就会惩罚你。

There are multiple ways to do this. What gets you here is that in order for you to receive the results parameter, the name for the generated edit controls should be results[0] for the first, results[1] for the second, etc with no gaps in the indexes (this is because of how DefaultModelBinder 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:

@Html.TextBox(string.Format("results[{0}]", i), res[i].text)

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:

class ItemViewModel
{
    Solution.Models.Data TheData { get; set; }
    Solution.Models.Results TheResults { get; set; }
}

You then make your view have a collection of these items as its model:

@model IEnumerable<ItemViewModel>

and then output the edit control with

Translation: @Html.EditorFor(modelItem => modelItem.TheResults)

Finally, you modify the postback action to accept an array of ItemViewModel:

[HttpPost] 
public ActionResult Controller(ItemViewModel[] results) 

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 through ViewBag or ViewData). If you do this, MVC will punish you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文