模型绑定列表为空

模型绑定列表为空

本文介绍了ASP.NET MVC 5 模型绑定列表为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个问题上停留了一段时间..

I stuck on this issue for a while..

我创建了一个简单的视图模型:

I've created a simple view model:

public class AddTranslationViewModel
{
    public List<ProjectTranslation> ProjectTranslations { get; set; }
    public AddTranslationViewModel()
    {
        ProjectTranslations = new List<ProjectTranslation>();
    }
}

ProjectTranslation 类:

ProjectTranslation class:

public class ProjectTranslation
{
    public int ProjectTranslationId { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string Address { get; set; }

    public int LanguageId { get; set; }
    public Language Language { get; set; }

    public int ProjectId { get; set; }
    public Project Project { get; set; }

}

使用 AddTranslationViewModel 的简单视图

A simple view which uses the AddTranslationViewModel

<table class="table">

    @foreach (var item in Model.ProjectTranslations)
    {
        @Html.HiddenFor(modelItem => item.ProjectTranslationId)
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Language.LanguageCode)
            </td>
            <td>
                @Html.EditorFor(modelItem => item.Title)
            </td>
        </tr>
    }

</table>
<input type="submit" value="Send" />

最后是我的 POST 方法:

and finally my POST Method:

    public ViewResult AddTranslation(AddTranslationViewModel projectTranslations)
    {
        if (ModelState.IsValid)
        {
           //...
        }
        return View(projectTranslations);
    }

这个想法非常基本,我想显示一个可以更改/编辑值的项目列表.

The idea is very basic, I want to show a list of items where it should be possible to change/edit the values.

但是,模型绑定不起作用,HTTPPost-Method AddTranslation 中的 projectsTranslations 参数始终为空.

However, the model binding is not working, the projectsTranslations param in the HTTPPost-Method AddTranslation is always empty.

这里有什么错误?

推荐答案

绑定到对象列表需要创建名称包含索引的输入字段结构,即:

Binding to a list of object requires creating input field structure with names containing indexes, i.e:

<input type="text" name="YourArrayOrList[0].SomeProperty" value="123" />
<input type="text" name="YourArrayOrList[0].SomeOtherProperty" value="321" />
<input type="text" name="YourArrayOrList[1].SomeProperty" value="123" />
<input type="text" name="YourArrayOrList[1].SomeOtherProperty" value="321" />

此外,您需要使用 Razor 的 Html.BeginFrom 方法(参见文档).在你的情况下,它应该是这样的:

Moreover, you need to point the form to the proper Action Method in your Controller using Razor's Html.BeginFrom method (see documentation).In you case it should look like this:

@using(Html.BeginForm("AddTranslation","YourControllerName"))
{
    for (int i=0;i<Model.ProjectTranslations.Count; i++)
    {
        @Html.HiddenFor(model => model.ProjectTranslations[i].ProjectTranslationId)
        <tr>
            <td>
                @Html.DisplayFor(model => model.ProjectTranslations[i].Language.LanguageCode)
            </td>
            <td>
                @Html.EditorFor(model => model.ProjectTranslations[i].Title)
            </td>
        </tr>
    }
}

如果你的方法不是 edit 而是 CREATE 方法,那么显然你的模型中的 List 将有 0 个元素.在这种情况下,将 for 循环中的停止条件更改为所需的计数.

请记住,这个话题之前已经讨论过很多次了:

Keep in mind that this topic was discussed many times before:

ASP.NET MVC 绑定模型中的数组

ASP.NET MVC - 无法将数组绑定到视图模型

这篇关于ASP.NET MVC 5 模型绑定列表为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 10:05