Set disable attribute based on a condition for Html.TextBoxFor

后端 未结 12 994
清酒与你
清酒与你 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:27

    I like the extension method approach so you don't have to pass through all possible parameters.
    However using Regular expressions can be quite tricky (and somewhat slower) so I used XDocument instead:

    public static MvcHtmlString SetDisabled(this MvcHtmlString html, bool isDisabled)
    {
        var xDocument = XDocument.Parse(html.ToHtmlString());
        if (!(xDocument.FirstNode is XElement element))
        {
            return html;
        }
    
        element.SetAttributeValue("disabled", isDisabled ? "disabled" : null);
        return MvcHtmlString.Create(element.ToString());
    }
    

    Use the extension method like this:
    @Html.EditorFor(m => m.MyProperty).SetDisabled(Model.ExpireDate == null)

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

    The valid way is:

    disabled="disabled"
    

    Browsers also might accept disabled="" but I would recommend you the first approach.

    Now this being said I would recommend you writing a custom HTML helper in order to encapsulate this disabling functionality into a reusable piece of code:

    using System;
    using System.Linq.Expressions;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Mvc.Html;
    using System.Web.Routing;
    
    public static class HtmlExtensions
    {
        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["disabled"] = "disabled";
            }
            return htmlHelper.TextBoxFor(expression, attributes);
        }
    }
    

    which you could use like this:

    @Html.MyTextBoxFor(
        model => model.ExpireDate, 
        new { 
            style = "width: 70px;", 
            maxlength = "10", 
            id = "expire-date" 
        }, 
        Model.ExpireDate == null
    )
    

    and you could bring even more intelligence into this helper:

    public static class HtmlExtensions
    {
        public static IHtmlString MyTextBoxFor<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression,
            object htmlAttributes
        )
        {
            var attributes = new RouteValueDictionary(htmlAttributes);
            var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            if (metaData.Model == null)
            {
                attributes["disabled"] = "disabled";
            }
            return htmlHelper.TextBoxFor(expression, attributes);
        }
    }
    

    so that now you no longer need to specify the disabled condition:

    @Html.MyTextBoxFor(
        model => model.ExpireDate, 
        new { 
            style = "width: 70px;", 
            maxlength = "10", 
            id = "expire-date" 
        }
    )
    
    0 讨论(0)
  • 2020-11-28 06:30

    One simple approach I have used is conditional rendering:

    @(Model.ExpireDate == null ? 
      @Html.TextBoxFor(m => m.ExpireDate, new { @disabled = "disabled" }) : 
      @Html.TextBoxFor(m => m.ExpireDate)
    )
    
    0 讨论(0)
  • 2020-11-28 06:31

    This is late, but may be helpful to some people.

    I have extended @DarinDimitrov's answer to allow for passing a second object that takes any number of boolean html attributes like disabled="disabled" checked="checked", selected="selected" etc.

    It will render the attribute only if the property value is true, anything else and the attribute will not be rendered at all.

    The custom reuseble HtmlHelper:

    public static class HtmlExtensions
    {
        public static IHtmlString MyTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
                                                                    Expression<Func<TModel, TProperty>> expression,
                                                                    object htmlAttributes,
                                                                    object booleanHtmlAttributes)
        {
            var attributes = new RouteValueDictionary(htmlAttributes);
    
            //Reflect over the properties of the newly added booleanHtmlAttributes object
            foreach (var prop in booleanHtmlAttributes.GetType().GetProperties())
            {
                //Find only the properties that are true and inject into the main attributes.
                //and discard the rest.
                if (ValueIsTrue(prop.GetValue(booleanHtmlAttributes, null)))
                {
                    attributes[prop.Name] = prop.Name;
                }                
            }                                
    
            return htmlHelper.TextBoxFor(expression, attributes);
        }
    
        private static bool ValueIsTrue(object obj)
        {
            bool res = false;
            try
            {
                res = Convert.ToBoolean(obj);
            }
            catch (FormatException)
            {
                res = false;
            }
            catch(InvalidCastException)
            {
                res = false;
            }
            return res;
        }
    
    }
    

    Which you can use like so:

    @Html.MyTextBoxFor(m => Model.Employee.Name
                       , new { @class = "x-large" , placeholder = "Type something…" }
                       , new { disabled = true})
    
    0 讨论(0)
  • 2020-11-28 06:37

    I achieved it using some extension methods

    private const string endFieldPattern = "^(.*?)>";
    
        public static MvcHtmlString IsDisabled(this MvcHtmlString htmlString, bool disabled)
        {
            string rawString = htmlString.ToString();
            if (disabled)
            {
                rawString = Regex.Replace(rawString, endFieldPattern, "$1 disabled=\"disabled\">");
            }
    
            return new MvcHtmlString(rawString);
        }
    
        public static MvcHtmlString IsReadonly(this MvcHtmlString htmlString, bool @readonly)
        {
            string rawString = htmlString.ToString();
            if (@readonly)
            {
                rawString = Regex.Replace(rawString, endFieldPattern, "$1 readonly=\"readonly\">");
            }
    
            return new MvcHtmlString(rawString);
        }
    

    and then....

    @Html.TextBoxFor(model => model.Name, new { @class= "someclass"}).IsDisabled(Model.ExpireDate == null)
    
    0 讨论(0)
  • 2020-11-28 06:39

    Another approach is to disable the text box on the client side.

    In your case you have only one textbox that you need to disable but consider the case where you have multiple input, select, and textarea fields that yout need to disable.

    It is much easier to do it via jquery + (since we can not rely on data coming from the client) add some logic to the controller to prevent these fields from being saved.

    Here is an example:

    <input id="document_Status" name="document.Status" type="hidden" value="2" />
    
    $(document).ready(function () {
    
        disableAll();
    }
    
    function disableAll() {
      var status = $('#document_Status').val();
    
      if (status != 0) {
          $("input").attr('disabled', true);
          $("textarea").attr('disabled', true);
          $("select").attr('disabled', true);
      }
    }
    
    0 讨论(0)
提交回复
热议问题