简体   繁体   English

如何避免重复的内联条件定义Html.EditorFor()的htmlAttributes

[英]How do I avoid repetitive inline conditionals for defining htmlAttributes of Html.EditorFor()

I am building a form and I have to keep using an inline conditional to add a readonly html attribute: 我正在构建一个表单,我必须继续使用内联条件来添加一个readonly html属性:

@Html.LabelFor(model => model.EventDate)
<div class="row">
    <div class="col-xs-3">
        @Html.EditorFor(model => model.EventDate, new
        {
            htmlAttributes = Model.IsEditorReadOnly ?
                (object)new { @class = "form-control input-lg", @type = "date", @readonly = "readonly" } :
                (object)new { @class = "form-control input-lg", @type = "date" }
        })
    </div>
</div>
@Html.ValidationMessageFor(model => model.EventDate)

You can't use a conditional for just the @readonly property's value because even if it is set to null, it will be rendered out to the client as readonly="" and that is enough for a browser to make that field read-only. 您不能仅对@readonly属性的值使用条件,因为即使将其设置为null,它也将以readonly=""呈现给客户端,这足以让浏览器将该字段设置为只读。

There has to be a better way to do this than an inline conditional for every form element just to add a single attribute, right? 必须有一个更好的方法来做这个,而不是每个表单元素的内联条件只是添加一个属性,对吗?

Thanks to Steven Muecke for all the assistance (give him all your up-votes above in the comments and at his linked answers ). 感谢Steven Muecke提供的所有帮助(在评论和他的链接 答案中给予他所有上述选票)。 Here's the solution . 这是解决方案

For a model with this property: 对于具有此属性的模型:

[Display(Name = "Event Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]
[Range(typeof(DateTime), "01-01-2010", "12-31-2030")]
public DateTime? EventDate { get; set; }

Create this extension method: 创建此扩展方法:

public static IHtmlString ReadOnlyEditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object
htmlAttributes = null, bool isReadOnly = false)
{
    IDictionary<string, object> attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
    if (isReadOnly)
    {
        attributes.Add("readonly", "readonly");
    }

    return html.EditorFor(expression, new { htmlAttributes = attributes });
}

And then use it in the view like this: 然后在视图中使用它:

@Html.ReadOnlyEditorFor(model => model.EventDate, 
    new { @class = "form-control input-lg", @type = "date" }, 
    Model.IsEditorReadOnly)

And all of the model's property's meta data will appear for the first instance that it is called on the page. 并且所有模型的属性的元数据将出现在页面上调用它的第一个实例。 The resulting html will look like this: 生成的html将如下所示:

<input class="form-control input-lg text-box single-line" data-val="true" data-val-date="The field Event Date must be a date." data-val-range="The field Event Date must be between 1/1/2010 12:00:00 AM and 12/31/2030 12:00:00 AM." data-val-range-max="12/31/2030 00:00:00" data-val-range-min="01/01/2010 00:00:00" id="EventDate" name="EventDate" type="date" value="08-01-2015" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM