2011-09-05 13 views
0
@Html.RadioButtonFor(modelItem => item.CheckerApproved, true) 
@Html.LabelFor(modelItem => item.CheckerApproved, "Accepted") 
@Html.RadioButtonFor(modelItem => item.CheckerApproved, false) 
@Html.LabelFor(modelItem => item.CheckerApproved, "Rejected") 

私が望むのは、ユーザーが認証されていないときにこれらを無効にしたり読み取り専用にする方法です。MVCラジオボタンは、認証されていないと読み取り専用になりません。

ie。有効な場合は

HttpContext.Current.User.Identity.IsAuthenticated 

これは簡単な方法はありますか?彼らをある種のパネルに置いてもらえますか? MVCが不明ですか?

各行に個別に有効化を設定しますか?

答えて

3

ユーザーが認証されていない場合、これらを無効にするか、読み取り専用にする方法があります。 カスタムヘルパーのための優れた候補より

より:

using System; 
using System.Linq.Expressions; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Mvc.Html; 

public static class RadioExtensions // LOL for this name 
{ 
    // TODO: Pick a better name here than MyRadioButtonFor 
    public static IHtmlString MyRadioButtonFor<TModel, TProperty>(
     this HtmlHelper<TModel> html, 
     Expression<Func<TModel, TProperty>> ex, 
     object value 
    ) 
    { 
     var isAuthenticated = html.ViewContext.HttpContext.User.Identity.IsAuthenticated; 
     if (isAuthenticated) 
     { 
      return html.RadioButtonFor(ex, value); 
     } 
     // Remark: adapt with readonly if necessary, 
     // note that there is a crucial difference between a readonly and disabled 
     // element in HTML. Up to you to pick the desired behavior 
     return html.RadioButtonFor(ex, value, new { @disabled = "disabled" }); 
    } 
} 

とあなたのビューでは、単にあなたの労働の成果を消費:

@Html.MyRadioButtonFor(modelItem => item.CheckerApproved, true) 
@Html.LabelFor(modelItem => item.CheckerApproved, "Accepted") 
@Html.MyRadioButtonFor(modelItem => item.CheckerApproved, false) 
@Html.LabelFor(modelItem => item.CheckerApproved, "Rejected") 
+0

非常にクール。私はそれがたくさん好きです。ご協力ありがとうございます! :) – AnonyMouse

+0

@AnonyMouse、あなたは大歓迎です。 –

関連する問題