如果派生的System.ComponentModel.DataAnnotations.ValidationAttribute存在,请从剃刀视图中检查

时间:2016-07-09 18:06:26

标签: c# asp.net-mvc razor validationattribute

我正在实施Html.EditorForModel(),以便它的Bootstrap友好。我有所有数据类型的编辑器模板,还有一个Object.cshtml,用<div class="form-control"></div>等包装每个编辑器模板。

问题是,当我在标记为[ComplexType]的模型上有属性时,我想列出子类的每个属性。但是,对于[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>
}

我的示例课程:

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; }
}

地址类:

[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; }
}

输出结果如何:

enter image description here

如图所示,我的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。如何访问此属性?有更优雅的解决方案吗?

1 个答案:

答案 0 :(得分:1)

我最终使用了UIHint:

[UIHint("HasChildProperties")]
public Address Address { get; set; }

if (prop.TemplateHint == "HasChildProperties") {...}