MVC复杂的模型继承,嵌套的ViewModels和部分意见的结合 [英] MVC Complex model binding with inheritance, nested viewModels and partial views

查看:184
本文介绍了MVC复杂的模型继承,嵌套的ViewModels和部分意见的结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不设法让我的嵌套模型的值返回到控制器,它们都是空值。

I don't manage to get the values of my nested models back to the controller, they are all nulls.

下面是简化的架构:

//The viewModel being passed to the view
public class RunnerIndexViewModel
{
    public RegisterViewModel User { get; set; }

    public TrainerViewModel TrainerVM { get; set; }

    public RunnerBase Runner { get; set; }

    [Display(Name = "AddContact", ResourceType = typeof(MyRessources))]
    public bool AddContact { get; set; }
}

public class RegisterViewModel
{
    // various simple type properties here
}

public class TrainerViewModel
{
    // various properties here
    public Unit unit { get; set; }

    public List<SelectListItem> ListStatut { get; set; }
}

public abstract partial class RunnerBase
{
    // entity framework class with associations
}

public class RedRunner : RunnerBase
{
    // entity framework class with associations
}

public class BlueRunner : RunnerBase
{
    // entity framework class with associations
}

下面是从控制器接收到模型的主要观点(简体):

Here is the main view that receives the model from the controller (simplified):

@model Web.Models.Fournisseurs.FournisseurIndexViewModel

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

    @Html.Partial("PartialTrainer", Model.TrainerVM)
    @Html.Partial("PartialRunner", Model.Runner as RedRunner)
    @Html.Partial("PartialUser", Model.User)
}

的意见PartialTrainer和PartialUser有没有什么特别的,所以这里是PartialRunner认为需要的基类的实体框架:

The views PartialTrainer and PartialUser have nothing special, so here is the PartialRunner view that takes the base class from entity framework:

@model RunnerBase

@* Show Various fields from RunnerBase ... *@

@if (Model is RedRunner)
{
    @* show properties specific to RedRunner *@
}
else if (Model is BlueRunner)
{
    @* show properties specific to BlueRunner *@
}   

从控制器我要么通过一个RedRunner或BlueRunner到亚军财产。所有的ViewModels所有字段中显示良好,但在提交表单的时候,我只设法获得的addContact值...

From the controller I either pass a RedRunner or a BlueRunner to the Runner property. All fields for all viewModels show up well but when submitting the form, I only manage to get the AddContact value...

我怎样才能得到我的其他的ViewModels的价值,并从亚军类的人?

How can I get the value of my other viewmodels and the one from the Runner class?

推荐答案

问题是用谐音。每次你进入一个部分时,该模型环境的变化,使剃须刀为您生成的输入没有完全占据属性路径考虑在内。例如,如果你是做以下的事情在你的主视图:

The problem is with using partials. Each time you go into the a partial, the model context changes, so the inputs that Razor generates for you are not taking the full property path into account. For example, if you were to do the following in your main view:

@Html.EditorFor(m => m.TrainerVM.SomeProperty)

产生的HTML将类似于:

The resulting HTML would be akin to:

<input type="text" name="TrainerVM.SomeProperty" />

然而,使用部分时,你的助手将被称为:

However, when using a partial, your helper would be called as:

@Html.EditorFor(m => m.SomeProperty)

和生成的HTML将类似于:

And the resulting HTML would be akin to:

<input type="text" name="SomeProperty" />

一旦被贴后背的ModelBinder的不知道 SomeProperty 属于 TrainerVM 实例,所以它不能确定在哪里张贴的值绑定,和你结束了零点。要继续使用谐音,但避开这个问题,您需要通过对 ViewData.TemplateInfo.HtmlField preFIX A值(这是用来正确preFIX在部分的所有字段)的名称:

Once this gets posted back, the modelbinder doesn't know that SomeProperty belongs to the TrainerVM instance so it can't determine where to bind the posted value, and you end up with nulls. To continue to use partials but side-step this problem, you'll need to pass a value for ViewData.TemplateInfo.HtmlFieldPrefix (which is used to properly prefix the names of all the fields in the partial):

@Html.Partial("PartialTrainer", Model.TrainerVM, new ViewDataDictionary(ViewData)
{
    TemplateInfo = new TemplateInfo { HtmlFieldPrefix = "TrainerVM" }
})

这篇关于MVC复杂的模型继承,嵌套的ViewModels和部分意见的结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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