Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8279035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:12:21+00:00 2026-06-08T09:12:21+00:00

I am trying to add a name attribute to my CheckBoxFor and can’t get

  • 0

I am trying to add a “name” attribute to my CheckBoxFor and can’t get it to work.

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

Any idea what I am doing wrong here?

Rendered HTML:

<div>
      <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>
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:12:23+00:00Added an answer on June 8, 2026 at 9:12 am

    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;
            }
            return;
        }
        else
        {
            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

    @Html.CheckBox("help_practicalSuggestions",Model.ProvidePracticalSuggestions)
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How comethis does not work? I am trying to add a new HTML attribute
I'm trying to add a name attribute to the User model provided by Devise.
Im new to asp.net mvc. I'm trying add new model class but it got
Trying to add a 'class' html attribute, but I think the keyword 'class' is
I am trying to add a WarningCheck attribute to my model, where I'd override
I am trying to add in a class with the name of autocomplete into
This code can't read a specific attribute - the name attribute more specifically. Instead
I am trying to add a dynamic Meta attribute to all of my Django
I am currently trying to add a custom column name to a property in
I'm trying to add a new FilteringSelect widget dynamically to a preexisting form I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.