user547794 user547794 - 1 year ago 98
C# Question

adding "name" attribute to CheckBoxFor

I am trying to add a "name" attribute to my

and can't get it to work.

model => model.ProvidePracticalSuggestions,
new { @name = "help_practicalSuggestions"

Any idea what I am doing wrong here?

Rendered HTML:

<input data-val="true" data-val-required="The Provide practical suggestions for implementing change, e.g. step-by-step best practices field is required." id="ProvidePracticalSuggestions" name="ProvidePracticalSuggestions" type="checkbox" value="true" /><input name="ProvidePracticalSuggestions" type="hidden" value="false" />
<span class="field-validation-valid" data-valmsg-for="ProvidePracticalSuggestions" data-valmsg-replace="true"></span>
<label for="ProvidePracticalSuggestions">Provide practical suggestions for implementing change, e.g. step-by-step best practices</label>

Answer Source

EDIT on 09/07/2016

This will not work

@Html.CheckBoxFor(model => model.ProvidePracticalSuggestions,
                                              new { name = "help_practicalSuggestions"   })

But This will work

@Html.CheckBoxFor(model => model.ProvidePracticalSuggestions,
                                              new { Name = "help_practicalSuggestions"   })

The trick is to use capital letter "N" in "Name"

I do not think this is Possible. You can not change the name attribute for a checkbox element when using the HTML Helper method. Out of curiousity, i looked into the MVC source code and found something interesting inside InputHelper method which is being called from the CheckBoxHelper method which is being called from the CheckBoxFor method

enter image description here

It is calling the MergeAttribute method for "type", "name" and "value". And this code is only for the Checkbox element

EDIT : Looked into the MergeAttribute method and this is how it is

public void MergeAttribute(string key, string value)
    bool flag = false;
    this.MergeAttribute(key, value, flag);

So it is calling MergeAttribute of the TagBuilder class and it looks like this

public void MergeAttribute(string key, string value, bool replaceExisting)
    if (!string.IsNullOrEmpty(key))
        if (replaceExisting || !this.Attributes.ContainsKey(key))
            this.Attributes[key] = value;
        throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "key");

And the the first If condition returns true and the inner if will not return true because ( I guess) name is present in the Attributes collection. So it will just execute the return statement.

Solution : Instead of using Html.CheckBoxFor helper method, you might use the Html.CheckBox method.

public static MvcHtmlString CheckBox(
    this HtmlHelper htmlHelper,
    string name

So your code will become


The first parameter is the name you want for the input element.

Also, as explained in one of the other answer , If you use new { @Name="MyCustomName"} and It will generate the checkbox element with the name you are passing. But it will not generate the hidden input field with this name. your hidden field will still have the same name as your model property name. CheckboxFor helper method usually generate the hidden field to store the selected/unselected value of checkbox in this hidden field. So it is important that you should have both field's(checkbox and it's associated hidden field) name as same.

So the best solution is to use Html.CheckBox helper method. This will generate the proper name for the hidden field as well.