我有下一个 View 模型:
public class PickUpLocationViewModel
{
public DateTime PuDate {get;set}
public IAddressViewModel {get;set;}
}
取决于 IAddressViewModel 的实现 我想使用适当的 UIHint("Airport")、UIHint("Seaport") 等。这可能吗?如果是,如何?
最佳答案
您可以像这样在 TemplateName 的 IAddressViewModel 上创建一个额外的属性:
public interface IAddressViewModel
{
string TemplateName { get; }
}
那么对于每个实现 IAddressViewModel 的类,您可以定义一个单独的模板名称,例如:
public class SeaportAddressViewModel : IAddressViewModel
{
public string TemplateName
{
get
{
return "Seaport";
}
}
}
然后在您的 View 中,您可以使用 EditorFor 的重载之一,例如:
@Html.EditorFor(m => m.Address, Model.Address.TemplateName)
这应该会导致它使用名为 Seaport.cshtml 的编辑器模板。
关于c# - 如何将 UIHint 属性用于接口(interface)的属性类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12057722/