简体   繁体   English

MVC 4模型绑定部分视图中使用的嵌套模型

[英]MVC 4 Model binding a nested model that is used in partial views

I got the following problem, when trying to bind a model in my controller method, the nested model is not bound (input name's do not match, because it's used in a partial view). 我遇到以下问题,当尝试在控制器方法中绑定模型时,未绑定嵌套模型(输入名称不匹配,因为在部分视图中使用了输入名称)。

Let me illustrate the problem with code samples: 让我用代码示例说明问题:

Controller: 控制器:

public class TestController : Controller
{
    public ActionResult Create()
    {
        var model = new Test2();
        model.Basisgegevens.Name = "Test";
        model.Basisgegevens.Omschrijving = "Omschrijving";

        return View(model);
    }

    [HttpPost]
    public ActionResult Create(Test2 model)
    {
        return View(model);
    }
}

Model: 模型:

public class Test
{
    public string Name { get; set; }

    public string Omschrijving { get; set; }
}

public class Test2
{
    public Test2()
    {
        this.Basisgegevens = new Test();
    }

    public int PeriodeVanId { get; set; }

    public int PeriodeTotId { get; set; }

    public Test Basisgegevens { get; set; }
}

View: 视图:

@model WebApplication4.Models.Test2

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Test2</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.PeriodeVanId, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PeriodeVanId)
                @Html.ValidationMessageFor(model => model.PeriodeVanId)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PeriodeTotId, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PeriodeTotId)
                @Html.ValidationMessageFor(model => model.PeriodeTotId)
            </div>
        </div>

        @Html.Partial("~/Views/Test/Partials/Naam.cshtml", Model.Basisgegevens)

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

Partial view: 部分视图:

@model WebApplication4.Models.Test

<div class="form-group">
    @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Omschrijving, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Omschrijving)
        @Html.ValidationMessageFor(model => model.Omschrijving)
    </div>
</div>

The model 'Test' used in the partial view is used by another controller, therefore i cannot changed the input field names (to allow binding). 在局部视图中使用的模型“测试”被另一个控制器使用,因此我不能更改输入字段名称(以允许绑定)。

This is sent to the server: PeriodeVanId:0 PeriodeTotId:0 Name:Test Omschrijving:Omschrijving 这将发送到服务器:PeriodeVanId:0 PeriodeTotId:0名称:Test Omschrijving:Omschrijving

I want the bottom 2 properties (from the nested model), to be renamed in the model binding at controller level to: Basisgegevens.Name Basisgegevens.Omschrijving 我希望将底部2个属性(来自嵌套模型)在控制器级别的模型绑定中重命名为:Basisgegevens.Name Basisgegevens.Omschrijving

That would allow for the binding, and then the model validation to kick in properly. 这样可以进行绑定,然后正确进行模型验证。

Does anyone know a solution for this simple model binding problem? 有人知道这个简单的模型绑定问题的解决方案吗?

You need to render the partial as an editor. 您需要将部分渲染为编辑器。 use the following 使用以下

@Html.EditorFor(model => model.Basisgegevens, "~/Views/Test/Partials/Naam.cshtml")

This will name the inputs correctly so they should bind. 这将正确命名输入,因此应进行绑定。 Basically it tells razor that the partial is part of the form not unrelated content 基本上它告诉剃刀部分是形式的一部分,与内容无关

For me it works when I use Html.EditorFor and I move partial view under folder EditorTemplates. 对我来说,当我使用Html.EditorFor并将部分视图移动到文件夹EditorTemplates下时,它可以工作。

There are 2 options where you need to create folder EditorTemplates: 您需要创建文件夹EditorTemplates的2个选项:

  • under particular view 在特定的观点下
  • or in share folder. 或在共享文件夹中。

在此处输入图片说明

在此处输入图片说明

Tested on MVC 5 app. 在MVC 5应用上测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM