开发者

Asp.net MVC dynamic Html attributes

开发者 https://www.devze.com 2023-02-07 14:22 出处:网络
I have few elements on my view textboxes , dropdowns etc.All of them have some unique attributes created like that

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 开发者_如何学JAVAthat


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.


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)%>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号