剃刀视图中的多视图模型

问题描述:

剃刀视图处理多个模型的最佳方式是什么?对于MVC3应用程序。剃刀视图中的多视图模型

我有两个型号,两者相似,但需要对邮编领域一个模型,不得为其他

public class IrelandPostcodeLookupViewModel , IWithProgress 
{ 
    readonly Progress _Progress = new Progress(Step.Delivery); 

    public Progress Progress 
    { 
     get { return _Progress; } 
    } 

    [Required(ErrorMessage = "Please enter your house number or name")] 
    [DisplayName("House number or name")] 
    public string HouseNumber { get; set; } 

    [StringLengthWithGenericMessage(50)] 
    [DisplayName("Eircode")] 
    public string Postcode { get; set; } 

} 

public class PostcodeLookupViewModel , IWithProgress 
{ 
    readonly Progress _Progress = new Progress(Step.Delivery); 

    public Progress Progress 
    { 
     get { return _Progress; } 
    } 

    [Required(ErrorMessage = "Please enter your house number or name")] 
    [DisplayName("House number or name")] 
    public string HouseNumber { get; set; } 

    [StringLengthWithGenericMessage(50)] 
    [Required(ErrorMessage = "Please enter your postcode")] 
    [DisplayName("PostCode")] 
    public string Postcode { get; set; } 

} 

在控制器我想请根据我通过一个国家的特定视图模型。喜欢的东西

public virtual ActionResult PostcodeLookup(string country) 
{ 
    if (country == Country.UnitedKingdom) 
     return View(new PostcodeLookupViewModel()); 
    else 
     return View(new IrelandPostcodeLookupViewModel()); 
} 

我在视图中处理这与

@model dynamic 

我的问题,这是我的看法包含部分景色

@Html.Partial("~/Views/Shared/_Progress.cshtml", Model.Progress) 

,我遇到错误“ HtmlHelper'没有名为'部分'的适用方法,但似乎有一个名称为扩展方法。扩展方法不能动态调度'

任何人都可以建议我如何处理部分视图?

由于

Model由于是dynamic,也Model.Progress产生dynamic
对于dynamic对象上的所有属性和函数调用,无论您走多深,都是如此。

为了解决这个问题,你可以类型转换Model.Progress对象:

@Html.Partial("~/Views/Shared/_Progress.cshtml", (Progress)Model.Progress)