开发者

TextBoxFor extension HTMLHelper

开发者 https://www.devze.com 2023-02-08 15:34 出处:网络
So I have this textbox on the page: <%= Html.TextBox(\"EffectiveDate\", Model.EffectiveDate.HasValue ? Model.EffectiveDate.Val开发者_如何学JAVAue.ToString(\"dd-MMM-yyyy\") : \"\", new { @class = \

So I have this textbox on the page:

<%= Html.TextBox("EffectiveDate", Model.EffectiveDate.HasValue ? Model.EffectiveDate.Val开发者_如何学JAVAue.ToString("dd-MMM-yyyy") : "", new { @class = "economicTextBox", propertyName = "EffectiveDate", onchange = "parseAndSetDt(this); ", dataType = "Date" })%>

It stores and displays a date using a date picker. Basically, depending on the users permission, I want this textbox to be disabled/enabled. This is fine, I can do this already, however, I want to make the textbox value = current date IF they don't have permission to change the value. I can use this HTML Extension to disable it w/ permissions:

public static MvcHtmlString TextBoxWithPermission(this HtmlHelper htmlHelper,
                                                  string name,
                                                  object value,
                                                  string[] permissions,
                                                  object htmlAttributes)
{
    foreach (string permission in permissions)
    {
        if (Chatham.Web.UI.Extranet.SessionManager.DisplayUser.IsInRole(permission))
        {
            // the user has the permission => render the textbox
            return htmlHelper.TextBox(name, value, htmlAttributes);
        }
    }

    // the user has no permission => render a readonly checkbox
    var mergedHtmlAttributes = new RouteValueDictionary(htmlAttributes);
    mergedHtmlAttributes["disabled"] = "disabled";
    return htmlHelper.TextBox(name, value, mergedHtmlAttributes);
}

But how do I fill it with the current date if it's disabled?


If I'm reading this right, you just want to pass the current date as the value in the no permissions case, instead of the value passed into TextBoxWithPermission?

// the user has no permission => render a readonly checkbox
var mergedHtmlAttributes = new RouteValueDictionary(htmlAttributes);
mergedHtmlAttributes["disabled"] = "disabled";
return htmlHelper.TextBox(name, DateTime.Now, mergedHtmlAttributes);
0

精彩评论

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