我有这样的模式视图,可以在其中设置属性的DataType注释。
AnyVal
Shared \ EditorTemplates文件夹包含这些模板。
public class Contact
{
[Required]
[Display(Name = "Your email address", Prompt = "Your email address")]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[Display(Name = "Your question, suggestion or feedback...", Prompt = "Your question, suggestion or feedback...")]
[DataType(DataType.MultilineText)]
public string Message { get; set; }
[Display(Name = "Your file", Prompt = "Your file")]
[DataType(DataType.Upload)]
public HttpPostedFileBase File { get; set; }
}
Object.cshtml模板用于将模型属性解析并将其重定向到其实际模板。
EmailAddress.cshtml
Upload.cshtml
MultilineText.cshtml
Object.cshtml
最后的代码是实际的View文件
@model dynamic
@foreach (var prop in ViewData.ModelMetadata.Properties.Where(p => p.ShowForEdit))
{
if (prop.TemplateHint == "HiddenInput")
{
@Html.Hidden(prop.PropertyName)
}
else
@Html.Editor(prop.PropertyName)
}
已成功选择并应用EmailAddress.cshtml和Upload.cshtml模板,但通过MultilineText.cshtml在属性上设置了MultilineText.cshtml,但没有成功。有什么想法吗?