Asp.net mvc 3 如何在Razor中重用强类型视图的部分?

Asp.net mvc 3 如何在Razor中重用强类型视图的部分?,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,我正在构建一个业务线应用程序,在呈现地址时,我有一段视图代码,我真的不想复制并粘贴到任何地方: <div class="editor-field"> @Html.DropDownListFor(model => model.Address.State, new SelectList(UsaStates.StateDictionary.OrderBy(s => s.Value), "Key", "Value", "Iowa"

我正在构建一个业务线应用程序,在呈现地址时,我有一段视图代码,我真的不想复制并粘贴到任何地方:

<div class="editor-field">
     @Html.DropDownListFor(model => model.Address.State, new
        SelectList(UsaStates.StateDictionary.OrderBy(s => s.Value), "Key", "Value",
            "Iowa"), "-- Select State --")
     @Html.ValidationMessageFor(model => model.Address.State)
</div>
模型类实现如下地址:

public class Business
{
   // .. other properties
   Address Address {get;set;}
}

public class College
{
   // .. other properties
   Address Address {get;set;}
}
有了它,我有两个视图,business/Create.cshtml和Colleges/Create.cshtml,它们有自己的模型


让我的应用程序中的所有视图(其模型具有地址字段)都可以使用此视图代码片段的最佳方法是什么?

您应该创建一个。

谢谢您的回答,但该示例并没有真正的帮助。这有点过时了,而且为了便于解释,它做了一些假设。在我的应用程序中,模型的Address属性在视图创建中没有搭建,因此UIHint属性无效。@Robert:我自己从来没有做过,所以我在这里无法真正帮助您,但您应该能够为
Address
类创建一个编辑器模板,并使用
EditorFor
调用它。你不需要在柱子上搭脚手架。您也可以编写一个局部视图。我从地址模型上的强类型局部视图开始,但我似乎不知道在RenderPartial()调用中作为模型传递什么,因为我一直得到一个“最佳重载方法匹配”异常。@Robert:您应该能够调用
Html.RenderPartial(“ViewName”,model.Address)
我已经缩小了范围
@Html.Partial(“\u Address”,Model.Address)
抛出“对象引用未设置为对象实例”错误
RenderPartial(“\u Address”,Model.Address)
引发“重载匹配”异常。调试剃须刀也是一种痛苦。
public class Business
{
   // .. other properties
   Address Address {get;set;}
}

public class College
{
   // .. other properties
   Address Address {get;set;}
}