简体   繁体   English

ASP.net MVC部分视图表单需要父模型的属性

[英]ASP.net MVC partial view form requiring property from parent model

I'm trying to structure my controllers and views in such a way that all of my views are strongly typed and I am not using ViewBag. 我正在尝试以这样的方式构造我的控制器和视图:我的所有视图都是强类型的,并且我没有使用ViewBag。 I have a base view model that I inherit from, create one "container" view model per view, plus a view model for each form. 我有一个继承自的基本视图模型,为每个视图创建一个“容器”视图模型,并为每个表单创建一个视图模型。

For this example let's focus on a Zone entity that can consist of a group of countries (might be used for calculating shipping, VAT, etc): 在此示例中,我们重点关注可以由一组国家组成的Zone实体(可能用于计算运费,增值税等):

public class BaseViewModel
{
    public string Title { get; set; }
    public string MetaDescription { get; set; }
    public string CanonicalUrl { get; set; }
    public Website Website { get; set; } // class that contains properties like site name, base url, use ssl, etc
}

public class ZoneCreateForm
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [DataType(DataType.MultiLineText)]
    public string Description { get; set; }

    public int[] CountryIds { get; set; } // Selected country ids
}

public class ZoneCreateViewModel : BaseViewModel
{
    public IEnumerable<Country> Countries { get; set; } // All countries. Used to create a dropdown list to select from

    public ZoneCreateForm CreateForm { get; set; }
}

I was trying to limit my "form models" to only the fields that are posted to the form. 我试图将“表单模型”限制为仅发布到表单的字段。 This helps scaffolding by eliminating the properties from the base model, and AutoMapper in the controller action on the post. 通过消除基础模型中的属性以及帖子上控制器操作中的AutoMapper,这有助于脚手架。

However, in this example I need access to the Countries property of the parent model in order to create the drop down list of all the countries. 但是,在此示例中,我需要访问父模型的Countries属性才能创建所有国家/地区的下拉列表。

The way I see it, I have some options: 我的看法是,我有一些选择:

  1. Give in and stuff the countries into the ViewBag so they are accessible from the CreateForm partial view. 将国家/地区塞入并放入ViewBag,以便可以从CreateForm部分视图访问它们。
  2. Make the Countries part of the ZoneCreateForm view model and use [Bind(Exclude="Countries")] on the controller action to avoid over-posting 使国家/地区成为ZoneCreateForm视图模型的一部分,并在控制器操作上使用[Bind(Exclude =“ Countries”)]以避免过分张贴
  3. Something else I'm not thinking of? 我没有想到的其他事情吗?

Is there a "standard" way of handling this? 是否有“标准”的处理方式? Most of the examples I've seen strewn across the web are using the domain models, rather than view-specific models with any inheritance and sub-models. 我在网上看到的大多数示例都是使用域模型,而不是具有继承和子模型的特定于视图的模型。 Whenever they have additional data needs they just stuff it into ViewBag, which seems dirty to me. 每当他们有其他数据需求时,就将其填充到ViewBag中,这对我来说似乎很脏。

What version of MVC you are using? 您正在使用哪个版本的MVC? With the MVC6 you can add dependency injection in view and not have this in your model. 使用MVC6,您可以在视图中添加依赖项注入,而在模型中则无需添加。

Example: 例:

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

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