editorfor 的 html 属性

发布于 2024-10-21 08:29:05 字数 63 浏览 1 评论 0原文

如何将 maxlength、style、css 等 html 属性添加到 Html.EditorFor() 中?

How can I add html attributes such as maxlength, style, css and ... to Html.EditorFor()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫瑾少女 2024-10-28 08:29:05

虽然已经晚了,但也许其他人会发现这很有帮助。

为什么要走那么远的路呢?我想我们正在处理一个字符串,因为您想添加一个 maxlength 属性。那么你可以只使用 Html.TextBoxFor 而不是 Html.Editorfor。

TextBoxFor 接受 html 属性。

@Html.TextBoxFor(model => model.Name, new{ maxlength = 50 })

This is way late but maybe someone else will find this helpful.

Why walk the long way? I suppose we are dealing with a string since you want to add a maxlength attribute. Then you can just use Html.TextBoxFor instead of Html.Editorfor.

TextBoxFor accepts html attributes.

@Html.TextBoxFor(model => model.Name, new{ maxlength = 50 })
天煞孤星 2024-10-28 08:29:05

今天我一直在努力解决同样的问题,由于我无法更改我的模型(而不是我的代码),所以我必须想出一种更好的方法来处理这个问题。这有点暴力,但它应该适用于我可能遇到的 99% 的情况。

在我的 Boolean.cshtml 编辑器模板中:

@model bool?

@{
    var attribs = new Dictionary<string, object>();
    var validAttribs = new string[] {"style", "class", "checked", "@class",
        "classname","id", "required", "value", "disabled", "readonly", 
        "accesskey", "lang", "tabindex", "title", "onblur", "onfocus", 
        "onclick", "onchange", "ondblclick", "onmousedown", "onmousemove", 
        "onmouseout", "onmouseover", "onmouseup", "onselect"};

    foreach (var item in ViewData) 
    {
        if (item.Key.ToLower().IndexOf("data_") == 0) 
        {
            attribs.Add(item.Key.Replace('_', '-'), item.Value);
        } 
        else 
        {
            if (validAttribs.Contains(item.Key.ToLower()))
            {
                attribs.Add(item.Key, item.Value);
            }
        }
    }
}

@Html.CheckBox("", Model.GetValueOrDefault(), attribs)

I've been wrestling with the same issue today, and since I can't change my model (not my code) I had to come up with a better way of handling this. It's a bit brute force, but it should work for 99% of cases I might encounter.

In my Boolean.cshtml editor template:

@model bool?

@{
    var attribs = new Dictionary<string, object>();
    var validAttribs = new string[] {"style", "class", "checked", "@class",
        "classname","id", "required", "value", "disabled", "readonly", 
        "accesskey", "lang", "tabindex", "title", "onblur", "onfocus", 
        "onclick", "onchange", "ondblclick", "onmousedown", "onmousemove", 
        "onmouseout", "onmouseover", "onmouseup", "onselect"};

    foreach (var item in ViewData) 
    {
        if (item.Key.ToLower().IndexOf("data_") == 0) 
        {
            attribs.Add(item.Key.Replace('_', '-'), item.Value);
        } 
        else 
        {
            if (validAttribs.Contains(item.Key.ToLower()))
            {
                attribs.Add(item.Key, item.Value);
            }
        }
    }
}

@Html.CheckBox("", Model.GetValueOrDefault(), attribs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文