Html Attributes with Asp.net MVC Attributes

I have several items in text boxes, dropdowns, etc. They all have some unique attributes created as

<%: Html.DropDownListFor(model => model.MyModel.MyType, EnumHelper.GetSelectList< MyType >(),new { @class = "someclass", @someattrt = "someattrt"})%> 

I would like to create a read-only version of my page by disabling another attribute.

Does anyone know how I can do this using a variable that can be set globally?

Sort of:

 If(pageReadOnly){ isReadOnlyAttr = @disabled = "disabled"; }else { isReadOnlyAttr ="" } <%: Html.DropDownListFor(model => model.MyModel.MyType, EnumHelper.GetSelectList< MyType >(),new { @class = "someclass", @someattrt = "someattrt",isReadOnlyAttr})%> 

I do not want to use JavaScript for this

+4
source share
2 answers

I did something similar to what you after I think - basically I have several different users of the system, and one set has read-only privileges on the website. For this, I have a variable for each view model:

 public bool Readonly { get; set; } 

which is installed at my model / business logic level depending on their privileges.

Then I created an extension for the DropDownListFor Html Helper, which takes a boolean value indicating whether only the drop-down list should read only:

 using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Web.Mvc; using System.Web.Mvc.Html; public static class DropDownListForHelper { public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> dropdownItems, bool disabled) { object htmlAttributes = null; if(disabled) { htmlAttributes = new {@disabled = "true"}; } return htmlHelper.DropDownListFor<TModel, TProperty>(expression, dropdownItems, htmlAttributes); } } 

Note that you can create other instances that also accept more parameters.

Than, in my opinion, I just imported the namespace for my html helper extension, and then passed the readDonly view variable to the DropDownListFor Html helper:

 <%@ Import Namespace="MvcApplication1.Helpers.HtmlHelpers" %> <%= Html.DropDownListFor(model => model.MyDropDown, Model.MyDropDownSelectList, Model.Readonly)%> 

I did the same for TextBoxFor, TextAreaFor, and CheckBoxFor, and they all all seem to work well. Hope this helps.

+5
source

Instead of disabling the drop-down list, why not replace it with the selected option ... if you are doing this for a lot of things, you should think about having read-only access and an editable view ...

 <% if (Model.IsReadOnly) { %> <%= Model.MyModel.MyType %> <% } else { %> <%= Html.DropDownListFor(model => model.MyModel.MyType, EnumHelper.GetSelectList< MyType >(),new { @class = "someclass", someattrt = "someattrt"})%> <% } %> 

And just as an aside, you only need to escape the attribute name with "@" if it is a reserved word, such as "class".

Update

Good. I have an answer for you - but on the condition that you read this before doing it.

MVC is all about sharing issues. Inclusion of logic in a controller, which is especially relevant to presentation, is an abuse of MVC. Please do not do this. Everything that is characteristic of the presentation, such as HTML, attributes, layout - none of this should be present in "controllerville". The controller should not change because you want to change something in the view.

It is very important that you understand what MVC is trying to achieve, and that the following example breaks the entire template and puts the presentation material in a completely wrong place in your application.

The correct fix will be to have a โ€œreadโ€ and โ€œeditโ€ view โ€” or put any conditional logic in the view. But here is a way to do what you want. :(

Add this property to the model.

 public IDictionary<string, object> Attributes { get; set; } 

In the controller you can conditionally set the attributes:

 model.Attributes = new Dictionary<string, object>(); model.Attributes.Add(@"class", "test"); if (isDisabled) { model.Attributes.Add("disabled", "true"); } 

Use attributes in your view:

 <%= Html.TextBoxFor(model => model.SomeValue, Model.Attributes)%> 
+4
source

Source: https://habr.com/ru/post/1337302/


All Articles