复杂型模型绑定 [英] Model binding with complex type

查看:94
本文介绍了复杂型模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个测试控制器和视图测试复杂的约束力,但我似乎无法使它工作。

I have made a test controller and view to test complex binding, but I can't seem to make it work.

下面是我的视图模型:

public class TestViewModel
{
    public SubTest MainTest { get; set; }

    public List<SubTest> SubTestList { get; set; }
}

public class SubTest
{
    public string Name { get; set; }
    public int Id { get; set; }
}

下面是我的看法:

@model TestViewModel    

@{
    using (Html.BeginForm())
    {
        <h2>Main</h2>

        <p>
            @Html.DisplayTextFor(m => m.MainTest.Id)
            =>
            @Html.DisplayTextFor(m => m.MainTest.Name)
        </p>

        <h2>Subs</h2>

        foreach (var sub in Model.SubTestList)
        {
            <p>
                @Html.DisplayTextFor(m => sub.Id)
                =>
                @Html.DisplayTextFor(m => sub.Name)
            </p>
        }

        <button type="submit">Submit</button>
    }
}

这是我的控制器:

And here is my controller:

public ActionResult Test()
{
    TestViewModel tvm = new TestViewModel();
    tvm.MainTest = new SubTest() { Id = 0, Name = "Main Test" };

    tvm.SubTestList = new List<SubTest>()
    {
        new SubTest() { Id = 1, Name = "Sub Test 1" } ,
        new SubTest() { Id = 2, Name = "Sub Test 2" } ,
        new SubTest() { Id = 3, Name = "Sub Test 3" } ,
        new SubTest() { Id = 4, Name = "Sub Test 4" } ,
    };

    return View(tvm);
}

[HttpPost]
public ActionResult Test(TestViewModel tvm)
{
    return View(tvm);
}

当我加载网页,一切都正确显示,但如果我设置了POST方法中设置断点,我看到的参数值都为null。

When I load the page, everything displays correctly, but if I set a breakpoint in the POST method, I see that the parameter values are both null.

我在做什么错了?

推荐答案

首先 DisplayTextFor()不会生成表单控件(输入,文本区域,选择),因此没有什么为表来发表了。

Firstly DisplayTextFor() does not generate form controls (input, textarea, select) therefore there is nothing for the form to post back.

其次,如果你也想编辑模型的值(比如用文本框),那么你就需要使用环(或自定义 EditorTemplate 为typeof运算分测验)不是的foreach 循环为您集合属性例如

Secondly, if you did want to edit the values of your model (say using a textbox), then you would need to use a for loop (or custom EditorTemplate for typeof SubTest) not a foreach loop for your collection property, for example

for (int i = 0; i < Model.SubTestList.Count; i++)
{
  @Html.TextBoxFor(m => m.SubTestList[i].Id)
  @Html.TextBoxFor(m => m.SubTestList[i].Name)
}

或使用 EditorTemplate (该模板的名称必须模型类型相匹配

Or using an EditorTemplate (the name of the template must match your model type

/View/Shared/EditorTemplates/SubTest.cshtml

@model yourAssembly.SubTest
@Html.TextBoxFor(m => m.Id)
@Html.TextBoxFor(m => m.Name)

和在主视图

@model TestViewModel 
....
@Html.EditorFor(m => m.SubTestList)

EditorFor()方法接受的IEnumerable&LT; T&GT; 和聪明足以呈现的HTML从模板对于集合中的每个项目

The EditorFor() method accepts IEnumerable<T> and is smart enough to rendered the html from the template for each item in the collection.

这篇关于复杂型模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆