8

I have few elements on my view textboxes , dropdowns etc. All of them have some unique attributes created like that

<%: 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 setting another attribute disabled.

Does anybody know how can I do it using variable that can be set globally?

Something like:

If(pageReadOnly){
isReadOnlyAttr  = @disabled = "disabled";
}else 
{
isReadOnlyAttr   =”” 
} 

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

I don’t want to use JavaScript to do that

2 Answers 2

7

I have done something similar to what you are after I think - basically I have a couple of different users of the system and one set have read-only privileges on the website. In order to do this I have a variable on each view model:

public bool Readonly { get; set; }

which is set in my model/business logic layer depending on their role privileges.

I then created an extension to the DropDownListFor Html Helper that accepts a boolean value indicating whether the drop-down list should be 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 take more parameters also.

Than in my view I simply imported the namespace for my html helper extension and then passed in the view model variable readonly 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 seem to work well. Hope this helps.

1
  • Hi Thanks for replay, yes this is what I'm after
    – Bart
    Jan 28, 2011 at 9:45
6

Rather than disabling the drop down list, why not replace it with the selected option... if you are doing this for a lot of stuff, you should think about having a read-only view 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

Okay. I do have an answer for you - but on the condition that you read this stuff before you implement it.

MVC is all about separating the concerns. Putting logic in the controller that is specifically a concern of the view is an abuse of MVC. Please don't do it. Anything specific to the view, like HTML, attributes, layout - none of that should ever feature in "controllerville". The controller shouldn't have to change because you want to change something in the view.

It is really important that you understand what MVC is trying to achieve and that the following example breaks the whole pattern and puts view stuff in entirely the wrong place in your application.

The correct fix would be to have a "read" view and an "edit" view - or to put any conditional logic in the view. But here is a way of doing 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 the attributes in your view:

<%= Html.TextBoxFor(model => model.SomeValue, Model.Attributes)%>
4
  • There is a lot of stuff on this view and we need it only for some kind of “demo” of what you can do. I’m trying to avoid creating a separate view or putting “if” statement on each element. I would rather remove the submit button from the form and add disabled attribute. Is that possible ?
    – Bart
    Jan 27, 2011 at 12:05
  • I have added an example that does this. Just because it is possible doesn't mean you have to do it... please read the stuff above the example.
    – Fenton
    Jan 27, 2011 at 13:53
  • Thanks for your update, that was my original approach , but different elements have different attributes so I can't pass the same model.Attributes to all of them.
    – Bart
    Jan 28, 2011 at 9:38
  • You can have different collections of attributes, or your could merge the dictionaries on the view with the disabled attribute dictionary on the model.
    – Fenton
    Jan 28, 2011 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.