ASP.Net MVC HtmlHelper.Checkbox
I'm trying to figure out how to alter the value of a checkbox in ASP.Net MVC. Basically what I want to do is have the value of a checkbox returned to the DefaultModelBinder so that I can use it in my controller method for manipulation. As of now, the values generated default to true/false for visible and hidden inputs respectively.
I want to change true/false to something else, for example name/false will either return the value name or empty string otherwise. I can do this manually with HTML code, but I'm curious to know how it can be done using the HTML Helpers. I've scoured the web and the MSDN, but haven't found开发者_运维百科 any good examples.
I know you can pass Object ^HTMLAttributes
as one of the parameters, but I'm not sure what the proper syntax is.
The values are fixed in the HtmlHelper code. You could always create your own helper using the source for the existing helper as a guide or hand-code the HTML. The relevant snippets are below:
public static string CheckBox(this HtmlHelper htmlHelper, string name, IDictionary<string, object> htmlAttributes) {
return htmlHelper.InputHelper(InputType.CheckBox, name, "true", true /* useViewData */, false /* isChecked */, true /* setId */, false /* isExplicitValue */, htmlAttributes);
}
public static string CheckBox(this HtmlHelper htmlHelper, string name, bool isChecked, IDictionary<string, object> htmlAttributes) {
// checked is an explicit parameter, but the value attribute is implicit so the dictionary's must take
// precedence.
RouteValueDictionary attributes = htmlAttributes == null ? new RouteValueDictionary() : new RouteValueDictionary(htmlAttributes);
attributes.Remove("checked");
return htmlHelper.InputHelper(InputType.CheckBox, name, "true", false /* useViewData */, isChecked, true /* setId */, false /* isExplicitValue */, attributes);
}
And from InputHelper():
if (inputType == InputType.CheckBox) {
// Render an additional <input type="hidden".../> for checkboxes. This
// addresses scenarios where unchecked checkboxes are not sent in the request.
// Sending a hidden input makes it possible to know that the checkbox was present
// on the page when the request was submitted.
StringBuilder inputItemBuilder = new StringBuilder();
inputItemBuilder.Append(tagBuilder.ToString(TagRenderMode.SelfClosing));
TagBuilder hiddenInput = new TagBuilder("input");
hiddenInput.MergeAttribute("type", HtmlHelper.GetInputTypeString(InputType.Hidden));
hiddenInput.MergeAttribute("name", name);
hiddenInput.MergeAttribute("value", "false");
inputItemBuilder.Append(hiddenInput.ToString(TagRenderMode.SelfClosing));
return inputItemBuilder.ToString();
}
精彩评论