mvc html禁用文本框,如何在MVC htmlAttribute中設置禁用

When using an HTML Helper, what is the best method to set an attribute based on a condition. For example

使用HTML Helper時,根據條件設置屬性的最佳方法是什么。例如

m.FirstName, new {@class='contactDetails'}%>

m.FirstName, new {@class='contactDetails', disabled = true}%>

There must be a better way to programmatically add just one additional KeyPair to the anonymous type? Can't use

必須有一種更好的方法來以編程方式向匿名類型添加一個額外的KeyPair嗎?不能用

new { .... disabled = Page.User.IsInRole("administrator") ... }

as the browser takes any disabled attribute value as making the input disabled

因為瀏覽器將任何禁用的屬性值視為禁用輸入

8 个解决方案

#1

14

I could suggest you to use mvccontrib.FluentHtml.

我建議你使用mvccontrib.FluentHtml。

You can do something like this

你可以做這樣的事情

m.FirstNam ).Disabled(Page.User.IsInRole("administrator"))%>

#2

12

It works for me as well...

它對我也有用......

model.IsActive, new { @disabled = "disabled", @readonly = "readonly" })%>

#3

8

Page.User.IsInRole("administrator") ? null : new { disabled = "disabled" }

Page.User.IsInRole(“管理員”)? null:new {disabled =“disabled”}

#4

4

Using @SLaks suggestion to use an Extension method, and using Jeremiah Clark's example Extension method I've written an extension method so I can now do

使用@SLaks建議使用Extension方法,並使用Jeremiah Clark的示例擴展方法我已經編寫了一個擴展方法,所以我現在可以做

Html.TextBoxFor(m => m.FirstName,new{class='contactDetails', ...},Page.User.IsInRole("administrator"));

Not Sure if there's a better method though

不確定是否有更好的方法

public static class InputExtensions

{

public static IDictionary TurnObjectIntoDictionary(object data)

{

var attr = BindingFlags.Public | BindingFlags.Instance;

var dict = new Dictionary();

if (data == null)

return dict;

foreach (var property in data.GetType().GetProperties(attr))

{

if (property.CanRead)

{

dict.Add(property.Name, property.GetValue(data, null));

}

}

return dict;

}

public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, Expression> expression, object htmlAttributes, bool disabled)

{

IDictionary values = TurnObjectIntoDictionary(htmlAttributes);

if (disabled)

values.Add("disabled","true");

return htmlHelper.TextBoxFor(expression, values);

}

public static MvcHtmlString TextAreaFor(this HtmlHelper htmlHelper, Expression> expression, object htmlAttributes, bool disabled)

{

IDictionary values = TurnObjectIntoDictionary(htmlAttributes);

if (disabled)

values.Add("disabled", "true");

return htmlHelper.TextAreaFor(expression, values);

}

public static MvcHtmlString CheckBoxFor(this HtmlHelper htmlHelper, Expression> expression, object htmlAttributes, bool disabled)

{

IDictionary values = TurnObjectIntoDictionary(htmlAttributes);

if (disabled)

values.Add("disabled", "true");

return htmlHelper.CheckBoxFor(expression, values);

}

}

#5

2

You'll need to pass a Dictionary, and add the disabled key inside an if statement.

您需要傳遞Dictionary

,並在if語句中添加disabled鍵。

I recommend making an overload of the extension method that takes a bool disabled parameter and adds it to a RouteValueDictionary created from the attributes parameter if it's true. (You could also remove the disabled entry from the RouteValueDictionary if it's false, and not take another parameter)

我建議對擴展方法進行重載,該方法接受bool禁用參數,並將其添加到從attributes參數創建的RouteValueDictionary中,如果它是真的。 (如果它是假的,你也可以從RouteValueDictionary中刪除禁用的條目,而不是取另一個參數)

#6

1

You may want to consider writing your own HtmlHelper Extension class with a new TextBox method:

您可能需要考慮使用新的TextBox方法編寫自己的HtmlHelper Extension類:

public static class HtmlHelperExtensions

{

public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, Expression> expression, string cssClass, bool disabled)

{

return disabled

? Html.TextBoxFor(expression, new {@class=cssClass, disabled="disabled"})

: Html.TextBoxFor(expression, new {@class=cssClass})

}

}

now (if this new class is in the same namespace, or you've imported the new namespace to your page header, or in the pages section of the web.config) you can do this on your aspx page:

now(如果這個新類在同一名稱空間中,或者您已將新名稱空間導入頁眉,或者在web.config的pages部分中),則可以在aspx頁面上執行此操作:

m.FirstName, "contactDetails", Page.User.IsInRole("administrator")) %>

#7

1

Created an extension method on Object that will create a copy of the input object excluding any properties that are null, and return it all as dictionary that makes it easily used in MVC HtmlHelpers:

在Object上創建了一個擴展方法,它將創建輸入對象的副本,排除任何null屬性,並將其全部作為字典返回,使其易於在MVC HtmlHelpers中使用:

public static Dictionary StripAnonymousNulls(this object attributes)

{

var ret = new Dictionary();

foreach (var prop in attributes.GetType().GetProperties())

{

var val = prop.GetValue(attributes, null);

if (val != null)

ret.Add(prop.Name, val);

}

return ret;

}

Not sure about performance implications of reflecting through properties twice, and don't like the name of the extension method much, but it seems to do the job well ...

不確定兩次反映屬性的性能影響,並且不太喜歡擴展方法的名稱,但似乎做得很好......

new {

@class = "contactDetails",

disabled = Page.User.IsInRole("administrator") ? "true" : null

}.StripAnonymousNulls()

#8

0

You may also define this param that way:

您也可以這樣定義此參數:

Page.User.IsInRole("administrator")

? (object)new { @class='contactDetails'}

: (object)new { @class='contactDetails', disabled = true}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值