如何通过html帮助器方法更改属性或将属性添加到html字段? [英] How to change or add attributes to html fields via html helper methods?

查看:169
本文介绍了如何通过html帮助器方法更改属性或将属性添加到html字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于布尔值禁用或启用文本框,我创建了此扩展方法:

I want to disable or enable a textbox based on boolean value, I created this extension method:

public static IHtmlString MyTextBoxFor<TModel,TProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel,TProperty>> expression,
            object htmlAttributes,
            bool disabled
            )
        {
            var attributes = new RouteValueDictionary(htmlAttributes);
            if (disabled)
            {
                attributes.Add("disabled", "\"disabled\"");
            }
            return htmlHelper.TextBoxFor(expression, htmlAttributes);
        }

那是我的用法:

        <div class="col-md-10">
            @Html.MyTextBoxFor(model => model.Body, new { @class = "form-control"}, true)
        </div>

但是它不起作用,我不是Htmlhelper类的新手,虽然不难理解,但是我确实错过了一些东西!

but its not working, I'm new to Htmlhelper class, though it's not hard to understand, but I certainly missed something!

我尝试了这种简单的方法,以找出问题所在:

I tried this simple method, to find out the problem:

public static IHtmlString MyTextBox(this HtmlHelper htmlHelper,object htmlAttributes, bool disabled)
        {
            IDictionary<string, object> attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
            //var attrs = new Dictionary<string,string>();
            if (disabled)
            {
                attrs.Add("disabled", "disabled");
                attrs.Add("value", "txxxxxxt");
            }
            return htmlHelper.TextBox("txtbx", attrs);
        }

那已经被渲染了: <input id="txtbx" name="txtbx" type="text" value="System.Collections.Generic.Dictionary``2[System.String,System.String]">

推荐答案

扩展方法的代码必须是

public static IHtmlString MyTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes, bool disabled)
{
    IDictionary<string, object> attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
    if (disabled)
    {
        attrs.Add("disabled", "disabled");
    }
    return htmlHelper.TextBoxFor(expression, attrs);
}

在您的第一个代码示例中,您对

In your first code example, your use of

return htmlHelper.TextBoxFor(expression, htmlAttributes);

返回的是原始属性,而不是包含disabled属性的更新属性.应该是

is returning the original attributes, not the updated attributes that includes the disabled attribute. It should have been

return htmlHelper.TextBoxFor(expression, attributes);

在第二个代码示例中,您使用TextBox()方法而不是TextBoxFor()和第二个参数是值,而不是属性,并且应该是

In your second code example, your using the TextBox() method rather that TextBoxFor() and the 2nd parameter is the value, not the attributes, and it should have been

return htmlHelper.TextBox("txtbx", null, attrs);

尽管由于不正确的name属性,该属性并没有绑定到您的属性.

although that would not have bound to your property because of the incorrect name attribute.

旁注:不清楚为什么要这么做.禁用的控件不会提交值,因此您也可以仅在视图中将属性的值呈现为文本.如果您确实希望提交其值,那么它应该是readonly,而不是disabled

Side note: Its a bit unclear why you would ever want to do this. Disabled controls do not submit a value so you may as well just render the value of the property as text in the view. If you do want its value to be submitted, then it should be readonly, not disabled

这篇关于如何通过html帮助器方法更改属性或将属性添加到html字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆