@Html.RadioButtonFor(modelItem => item.CheckerApproved, true)
@Html.LabelFor(modelItem => item.CheckerApproved, "Accepted")
@Html.RadioButtonFor(modelItem => item.CheckerApproved, false)
@Html.LabelFor(modelItem => item.CheckerApproved, "Rejected")
我想要的是某种方式在用户未经过身份验证时将这些禁用或只读。
即。在以下时间启用:
HttpContext.Current.User.Identity.IsAuthenticated
有一种简单的方法吗?你会把它们放在某种面板上吗?对MVC不确定?
您是否会为每一行单独设置启用功能?
答案 0 :(得分: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")