Html.Textbox 的 HTML Helper 扩展方法
因此,我有一个 Html.CheckBoxFor() 方法的扩展方法,该方法使用户能够传递如下所示的权限数组:
<%= Html.CheckBoxForWithPermission(m => m.Current, new string[] { PERMISSIONS.hasICAdvanced }, new { @class = "economicTextBox", propertyName = "Current", onchange = "UseCurrent();UpdateField(this);" })%>
该方法如下所示:
public static MvcHtmlString CheckBoxForWithPermission<TModel>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, bool>> expression,
string[] permissions,
object htmlAttributes
)
{
foreach (string permission in permissions)
{
if (Chatham.Web.UI.Extranet.SessionManager.PhysicalUser.IsInRole(permission))
{
// the user has the permission => render the checkbox
return htmlHelper.CheckBoxFor(expression, htmlAttributes);
}
}
// the user has no permission => render a readonly checkbox
var mergedHtmlAttributes = new RouteValueDictionary(htmlAttributes);
mergedHtmlAttributes["disabled"] = "disabled";
return htmlHelper.CheckBoxFor(expression, mergedHtmlAttributes);
}
基本上,我想创建除了 Html.TextBox 之外完全相同的东西我们目前这样调用的方法:
<%= Html.TextBox("RateTimeStamp", Model.RateTimeStamp.HasValue ? Model.RateTimeStamp.Value.ToString("dd-MMM-yyyy") : "", new { @class = "economicTextBox", propertyName = "RateTimeStamp", onchange = "parseAndSetDt(this);", dataType = "Date" })%>
由于这个助手有点不同,我不太确定如何格式化该方法。
任何帮助将不胜感激。谢谢!
So I have an extension method for the Html.CheckBoxFor() method that enables the user to pass in an array of permissions like this:
<%= Html.CheckBoxForWithPermission(m => m.Current, new string[] { PERMISSIONS.hasICAdvanced }, new { @class = "economicTextBox", propertyName = "Current", onchange = "UseCurrent();UpdateField(this);" })%>
The method looks like this:
public static MvcHtmlString CheckBoxForWithPermission<TModel>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, bool>> expression,
string[] permissions,
object htmlAttributes
)
{
foreach (string permission in permissions)
{
if (Chatham.Web.UI.Extranet.SessionManager.PhysicalUser.IsInRole(permission))
{
// the user has the permission => render the checkbox
return htmlHelper.CheckBoxFor(expression, htmlAttributes);
}
}
// the user has no permission => render a readonly checkbox
var mergedHtmlAttributes = new RouteValueDictionary(htmlAttributes);
mergedHtmlAttributes["disabled"] = "disabled";
return htmlHelper.CheckBoxFor(expression, mergedHtmlAttributes);
}
Basically, I want to create the exact same thing except for an Html.TextBox method that we currently call like this:
<%= Html.TextBox("RateTimeStamp", Model.RateTimeStamp.HasValue ? Model.RateTimeStamp.Value.ToString("dd-MMM-yyyy") : "", new { @class = "economicTextBox", propertyName = "RateTimeStamp", onchange = "parseAndSetDt(this);", dataType = "Date" })%>
Since this helper is a little bit different I'm not really sure how to format the method.
Any help would be greatly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
然后:
如果您想要具有可以使用非类型化助手的格式:
并像这样调用:
and then:
and if you want to have the format you could use untyped helpers:
and call like this: