Set disable attribute based on a condition for Html.TextBoxFor

后端 未结 12 995
清酒与你
清酒与你 2020-11-28 06:13

I want to set disable attribute based on a condition for Html.TextBoxFor in asp.net MVC like below

@Html.TextBoxFor(model => model.ExpireDate, new { style         


        
相关标签:
12条回答
  • 2020-11-28 06:40

    Is solved this using RouteValueDictionary (works fine as htmlAttributes as it's based on IDictionary) and an extension method:

    public static RouteValueDictionary AddIf(this RouteValueDictionary dict, bool condition, string name, object value)
    {
        if (condition) dict.Add(name, value);
        return dict;
    }
    

    Usage:

    @Html.TextBoxFor(m => m.GovId, new RouteValueDictionary(new { @class = "form-control" })
    .AddIf(Model.IsEntityFieldsLocked, "disabled", "disabled"))
    

    Credit goes to https://stackoverflow.com/a/3481969/40939

    0 讨论(0)
  • 2020-11-28 06:41

    Actually, the internal behavior is translating the anonymous object to a dictionary. So what I do in these scenarios is go for a dictionary:

    @{
      var htmlAttributes = new Dictionary<string, object>
      {
        { "class" , "form-control"},
        { "placeholder", "Why?" }        
      };
      if (Model.IsDisabled)
      {
        htmlAttributes.Add("disabled", "disabled");
      }
    }
    @Html.EditorFor(m => m.Description, new { htmlAttributes = htmlAttributes })
    

    Or, as Stephen commented here:

    @Html.EditorFor(m => m.Description,
        Model.IsDisabled ? (object)new { disabled = "disabled" } : (object)new { })
    
    0 讨论(0)
  • 2020-11-28 06:42

    I like Darin method. But quick way to solve this,

    Html.TextBox("Expiry", null, new { style = "width: 70px;", maxlength = "10", id = "expire-date", disabled = "disabled" }).ToString().Replace("disabled=\"disabled\"", (1 == 2 ? "" : "disabled=\"disabled\""))
    
    0 讨论(0)
  • 2020-11-28 06:43

    Yet another solution would be to create a Dictionary<string, object> before calling TextBoxFor and pass that dictionary. In the dictionary, add "disabled" key only if the textbox is to be diabled. Not the neatest solution but simple and straightforward.

    0 讨论(0)
  • 2020-11-28 06:46

    If you don't use html helpers you may use simple ternary expression like this:

    <input name="Field"
           value="@Model.Field" tabindex="0"
           @(Model.IsDisabledField ? "disabled=\"disabled\"" : "")>
    
    0 讨论(0)
  • 2020-11-28 06:50

    if you dont want to use Html Helpers take look it my solution

    disabled="@(your Expression that returns true or false")"
    

    that it

    @{
        bool isManager = (Session["User"] as User).IsManager;
    }
    <textarea rows="4" name="LetterManagerNotes" disabled="@(!isManager)"></textarea>
    

    and I think the better way to do it is to do that check in the controller and save it within a variable that is accessible inside the view(Razor engine) in order to make the view free from business logic

    0 讨论(0)
提交回复
热议问题