Razor 视图中的多个视图模型

Multiple view models in Razor view

让剃刀视图处理多个模型的最佳方法是什么?对于 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)

and I 运行 into the error 'HtmlHelper' has no applicable method named 'Partial' but appears to have an extension method by the name.无法动态调度扩展方法'

谁能告诉我如何处理局部视图?

谢谢

因为 Modeldynamic,所以 Model.Progress 也会产生 dynamic
dynamic 对象上的所有属性和函数调用都是如此,无论你深入到什么程度。

要解决此问题,您可以对 Model.Progress 对象进行类型转换:

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