【问题标题】:Check from razor view if derived System.ComponentModel.DataAnnotations.ValidationAttribute exists从剃刀视图检查派生的 System.ComponentModel.DataAnnotations.ValidationAttribute 是否存在
【发布时间】:2016-11-12 02:13:31
【问题描述】:

我正在实施 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; }
}

以及输出的样子:

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

【问题讨论】:

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


    【解决方案1】:

    我最终使用了 UIHint:

    [UIHint("HasChildProperties")]
    public Address Address { get; set; }
    
    if (prop.TemplateHint == "HasChildProperties") {...}
    

    【讨论】:

      猜你喜欢
      • 2012-12-10
      • 2021-08-11
      • 2012-01-25
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多