如何在 asp.net-mvc 中的 html 文本框上设置禁用属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3443460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:03:06  来源:igfitidea点击:

how do I set disabled attribute on html textbox in asp.net-mvc?

htmlasp.net-mvctextbox

提问by leora

I am trying to dynamically set the disabled attribute on the html textbox and having issues

我正在尝试在 html 文本框上动态设置 disabled 属性并遇到问题

I tried this in my view:

我在我看来试过这个:

 string disabledString = "";
 if (SomeLogic)
 {
      disabledString = "disabled";
 }

 Html.Textbox()...new Dictionary<string, object> { { "maxlength", 50 }, { "disabled", readOnlyState } })%>

As you can see I am setting the disabled attribute to "" or disabled but when I test, it seems to be disabled in either case. Am I missing something?

正如您所看到的,我将 disabled 属性设置为 "" 或 disabled 但当我测试时,它似乎在任何一种情况下都被禁用。我错过了什么吗?

回答by Robaticus

This was ugly for us, due to the fact that the HTML spec is lousy here.

这对我们来说很难看,因为这里的 HTML 规范很糟糕。

Basically in our view code we had some logic like this:

基本上在我们的视图代码中,我们有一些这样的逻辑:

bool isPlatformOwner = false;

object disabledAttributes = new { @disabled="disabled", @readonly="readonly" };

//omitted code setting isPlatformOwner    

    if (isPlatformOwner)
    {
        disabledAttributes = new { };
    }

Then, for our controls, we had this:

然后,对于我们的控件,我们有这个:

<%=Html.CheckBoxFor(f => f.AddToReleaseIndicator, disabledAttributes)%>

Anonymous types saved us here, but, like I said, it got a little ugly.

匿名类型在这里救了我们,但是,就像我说的,它有点难看。

回答by Cagatay Kalan

Actually it is possible to write an Extension class to the HtmlHelper to do this but you have to implement many overrides so the quickest solution I found was to write a dictionary extension.

实际上,可以为 HtmlHelper 编写一个 Extension 类来执行此操作,但是您必须实现许多覆盖,因此我找到的最快解决方案是编写一个字典扩展。

You can use below class for this:

您可以为此使用以下类:

public static class DictionaryExtensions
{
    public static Dictionary<string, object> WithAttrIf(this Dictionary<string,object> dictionary,bool condition, string attrname, object value)
    {
        if (condition)
            dictionary[attrname] = value;

        return dictionary;
    }

    public static Dictionary<string, object> WithAttr(this Dictionary<string, object> dictionary, string attrname, object value)
    {

        dictionary[attrname] = value;

        return dictionary;
    }
}

To use it, import the class in your view and your view code looks like this:

要使用它,请在您的视图中导入该类,您的视图代码如下所示:

@Html.TextBoxFor(m => m.FirstName,  new Dictionary<string, object>().WithAttr("class","input-large").WithAttrIf(!string.IsNullOrWhiteSpace(Model.FirstName),"readonly","yes"))

You can add as many attributes as you wish since the extension method adds the value to the dictionary and returns the dictionary itself

您可以添加任意数量的属性,因为扩展方法将值添加到字典中并返回字典本身

回答by Dismissile

I think you want to omit the disabled attribute altogether when you want it to be enabled. Older browsers would look at the following and disable the text boxes:

我认为当您希望启用它时,您希望完全省略禁用属性。较旧的浏览器会查看以下内容并禁用文本框:

<input type="text" disabled></input>

In other words in older HTML the ="disabled" was not necessary so for compatibility reasons you should just omit the attribute if you want it to render right. I'm not sure what happens if you try a strict DOCTYPE, though.

换句话说,在旧的 HTML 中 ="disabled" 不是必需的,因此出于兼容性原因,如果您希望它正确呈现,您应该省略该属性。不过,我不确定如果您尝试严格的 DOCTYPE 会发生什么。