从 razor 视图检查派生 System.ComponentModel.DataAnnotations.ValidationAttribute 是否存在
Check from razor view if derived System.ComponentModel.DataAnnotations.ValidationAttribute exists
我正在实施 Html.EditorForModel()
以便 Bootstrap 友好。我有适用于所有数据类型的编辑器模板,还有一个 Object.cshtml,它用 <div class="form-control"></div>
等
包装每个编辑器模板
问题是,当我在标记为 [ComplexType]
的模型上有一个 属性 时,我想列出 child [的每个 属性 class。然而,对于 [DataType(DataType.Upload)]
,它是一种 HttpPostedFileBase
数据类型,这工作正常,prop.IsComplexType
将其视为复杂数据类型并最终列出其属性而不是呈现 <input type="file" />
这是Object.cshtml:
@model dynamic
@{
var modelMetaData = ViewData.ModelMetadata;
var properties = modelMetaData.Properties;
}
@foreach (var prop in properties.Where(p => p.ShowForEdit))
{
string propertyName = prop.PropertyName;
if (prop.TemplateHint == "HiddenInput")
{
@Html.Hidden(propertyName)
}
else
{
if (prop.IsComplexType)
{
<fieldset>
<legend>@propertyName</legend>
<div class="mt-ml-2em">
@foreach (var subProp in prop.Properties)
{
var propertyName1 = subProp.PropertyName;
string fullname = propertyName + "." + propertyName1;
<div class="form-group">
@Html.BootstrapLabel(propertyName1)
@Html.Editor(fullname, MyHelpers.TemplateHelpers.GetTemplateForProperty(subProp))
<p class="help-block">@subProp.Description</p>
@Html.ValidationMessage(fullname, new { @class = "color-red" })
</div>
}
</div>
</fieldset>
}
else
{
<div class="form-group">
@Html.BootstrapLabel(propertyName)
@Html.Editor(propertyName, MyHelpers.TemplateHelpers.GetTemplateForProperty(prop))
<p class="help-block">@prop.Description</p>
@Html.ValidationMessage(propertyName, new { @class = "color-red" })
</div>
}
}
}
我的剃刀观点:
@model MyMvc45Template.Areas.SampleTests.Models.Product
@{
ViewBag.Title = "ForModel";
}
@*<h2>BsFormGroupFor</h2>
@Html.BsFormGroupFor(m => m.ProductName)*@
<h2>For Model Test</h2>
@using (Html.BeginForm("ForModel", "FormTests", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.EditorForModel()
<div class="form-group">
<input type="submit" value="Save" class="btn btn-success" />
</div>
}
我的样本 classes:
public class Product
{
public int Id { get; set; }
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[HasChildProperties] //I am thinking I can use this custom ValidationAttribute as a flag in Object.cshtml
public Address Address { get; set; }
[DataType(DataType.PhoneNumber)]
public string Phone { get; set; }
[DataType(DataType.Upload)]
public HttpPostedFileBase File { get; set; }
[DataType(DataType.Url)]
public string Url { get; set; }
}
地址class:
[ComplexType]
public class Address
{
[Display(Description = "This is the help block text")]
public string Line1 { get; set; }
public string Line2 { get; set; }
public string City { get; set; }
}
以及输出结果:
如图所示,我的 File
属性 最终列出了它的成员,而不是呈现我的 Upload.cshtml
:
@model dynamic
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
new { @class = "form-control", placeholder = ViewData.ModelMetadata.Watermark,
type = "file" })
我想我可以使用这个 ValidationAttribute 作为 Object.cshtml
中的标志:
/// <summary>
/// This will make the editor template for Object.cshtml list any child properties in an edit form
/// </summary>
public class HasChildProperties : ValidationAttribute
{
}
但我在元数据中找不到我的自定义 ValidationAttribute
。我怎样才能访问这个属性?有更优雅的解决方案吗?
我最终使用了 UIHint:
[UIHint("HasChildProperties")]
public Address Address { get; set; }
if (prop.TemplateHint == "HasChildProperties") {...}
我正在实施 Html.EditorForModel()
以便 Bootstrap 友好。我有适用于所有数据类型的编辑器模板,还有一个 Object.cshtml,它用 <div class="form-control"></div>
等
问题是,当我在标记为 [ComplexType]
的模型上有一个 属性 时,我想列出 child [的每个 属性 class。然而,对于 [DataType(DataType.Upload)]
,它是一种 HttpPostedFileBase
数据类型,这工作正常,prop.IsComplexType
将其视为复杂数据类型并最终列出其属性而不是呈现 <input type="file" />
这是Object.cshtml:
@model dynamic
@{
var modelMetaData = ViewData.ModelMetadata;
var properties = modelMetaData.Properties;
}
@foreach (var prop in properties.Where(p => p.ShowForEdit))
{
string propertyName = prop.PropertyName;
if (prop.TemplateHint == "HiddenInput")
{
@Html.Hidden(propertyName)
}
else
{
if (prop.IsComplexType)
{
<fieldset>
<legend>@propertyName</legend>
<div class="mt-ml-2em">
@foreach (var subProp in prop.Properties)
{
var propertyName1 = subProp.PropertyName;
string fullname = propertyName + "." + propertyName1;
<div class="form-group">
@Html.BootstrapLabel(propertyName1)
@Html.Editor(fullname, MyHelpers.TemplateHelpers.GetTemplateForProperty(subProp))
<p class="help-block">@subProp.Description</p>
@Html.ValidationMessage(fullname, new { @class = "color-red" })
</div>
}
</div>
</fieldset>
}
else
{
<div class="form-group">
@Html.BootstrapLabel(propertyName)
@Html.Editor(propertyName, MyHelpers.TemplateHelpers.GetTemplateForProperty(prop))
<p class="help-block">@prop.Description</p>
@Html.ValidationMessage(propertyName, new { @class = "color-red" })
</div>
}
}
}
我的剃刀观点:
@model MyMvc45Template.Areas.SampleTests.Models.Product
@{
ViewBag.Title = "ForModel";
}
@*<h2>BsFormGroupFor</h2>
@Html.BsFormGroupFor(m => m.ProductName)*@
<h2>For Model Test</h2>
@using (Html.BeginForm("ForModel", "FormTests", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.EditorForModel()
<div class="form-group">
<input type="submit" value="Save" class="btn btn-success" />
</div>
}
我的样本 classes:
public class Product
{
public int Id { get; set; }
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[HasChildProperties] //I am thinking I can use this custom ValidationAttribute as a flag in Object.cshtml
public Address Address { get; set; }
[DataType(DataType.PhoneNumber)]
public string Phone { get; set; }
[DataType(DataType.Upload)]
public HttpPostedFileBase File { get; set; }
[DataType(DataType.Url)]
public string Url { get; set; }
}
地址class:
[ComplexType]
public class Address
{
[Display(Description = "This is the help block text")]
public string Line1 { get; set; }
public string Line2 { get; set; }
public string City { get; set; }
}
以及输出结果:
如图所示,我的 File
属性 最终列出了它的成员,而不是呈现我的 Upload.cshtml
:
@model dynamic
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
new { @class = "form-control", placeholder = ViewData.ModelMetadata.Watermark,
type = "file" })
我想我可以使用这个 ValidationAttribute 作为 Object.cshtml
中的标志:
/// <summary>
/// This will make the editor template for Object.cshtml list any child properties in an edit form
/// </summary>
public class HasChildProperties : ValidationAttribute
{
}
但我在元数据中找不到我的自定义 ValidationAttribute
。我怎样才能访问这个属性?有更优雅的解决方案吗?
我最终使用了 UIHint:
[UIHint("HasChildProperties")]
public Address Address { get; set; }
if (prop.TemplateHint == "HasChildProperties") {...}