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 6165373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:09:08+00:00 2026-05-23T22:09:08+00:00

In an EditorFor view in MVC with Razor markup, if you do this: @Html.TextBox()

  • 0

In an EditorFor view in MVC with Razor markup, if you do this:

@Html.TextBox("")

The HTML comes out with the correct input name for the model, like so:

<input id="usr_Roles" name="usr.Roles" type="text" value="">

The “usr.Roles” name comes from this EditorFor line:

@Html.EditorFor(modelItem => usr.Roles, "RoleList") 

Unfortunately, I’m trying to generate a set of checkboxes with the name “usr.Roles”. Because of the way MVC renders checkboxes, I can’t use Html.Checkbox. I can generate them manually like so:

@foreach (string roleName in Roles.GetAllRoles())
{
    string checkboxId = "checkbox_" + roleName;
    <input type="checkbox" name="usr.Roles" id="@checkboxId" value="@roleName" @Html.Raw(Model.Contains(roleName) ? "checked" : "") />
    <label for="@checkboxId">@roleName</label>
} 

The above works, but only because I manually specify “usr.Roles” as the checkbox name.

My question is this: Where in the maze of objects and properties does the @Html.TextBox object find the “usr.Roles” “usr-Roles” strings? And can I find it myself and use it in the checkboxes?

  • 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-05-23T22:09:09+00:00Added an answer on May 23, 2026 at 10:09 pm

    I don’t know why on earth you would be wanting to manually generate ids and names of your checkboxes instead of simply using the Html.CheckBoxFor helper which does all this for you, but in case you have some reasons, here’s how:

    @{
        var name = ViewData.TemplateInfo.GetFullHtmlFieldName("");
    }
    
    @foreach (string roleName in Roles.GetAllRoles())
    {
        string checkboxId = "checkbox_" + roleName;
        <input type="checkbox" name="@name" id="@checkboxId" value="@roleName" @Html.Raw(Model.Contains(roleName) ? "checked" : "") />
        <label for="@checkboxId">@roleName</label>
    } 
    

    Obviously that’s shown here pure for some educational purposes. I would really avoid such monstrosity and simply use Html helpers and editor templates. Another thing that shocks me in your view is the following line of code: Roles.GetAllRoles(). It’s as if your view is querying data. That’s last thing a view should do. It’s the controller’s responsibility to query data, fill a view model and pass this view model to the view so that it simply shows the data.

    So let’s try to elaborate your example. From what I can understand you are trying to show a list of checkboxes for each role so that the user can select them.

    As always you start with a view model expressing your views requirements:

    public class MyViewModel
    {
        public IEnumerable<RoleViewModel> Roles { get; set; }
    }
    
    public class RoleViewModel
    {
        public string RoleName { get; set; }
        public bool Selected { get; set; }
    }
    

    then you write a controller action which will query your data and fill the view model:

    public ActionResult Index()
    {
        var roles = Roles.GetAllRoles();
        var model = new MyViewModel
        {
            Roles = roles.Select(role => new RoleViewModel
            {
                RoleName = role,
                Selected = ??????
            })
        };
        return View(model);
    }
    

    then you will have a Index.cshtml view:

    @model MyViewModel
    @Html.EditorFor(x => x.Roles)
    

    and a corresponding editor template which will be rendered for each element of the Roles collection (~/View/Shared/EditorTemplates/RoleViewModel.cshtml):

    @model RoleViewModel
    @Html.CheckBoxFor(x => x.Selected)
    @Html.LabelFor(x => x.Selected, Model.RoleName)
    

    No more foreach loops in the views, no more ugly hacks with names and ids, no more data pulling from the views.

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

Sidebar

Related Questions

I am just new to MVC. when we use @Html.EditorFor in razor view, it
I have a basic MVC view model with annotations, for example: [Required(ErrorMessage=Your Name Required)]
This one is strange...I have the following markup for a view using Razor view
<td>@Html.EditorFor(Model => Model.Loan)</td> I have that in the beginning of the view, and then
I have created one page in MVC 3.0 Razor view. Create.cshtml @model LiveTest.Business.Models.QuestionsModel @*<script
I am getting some unexpected behavior from Html.EditorFor(). I have this controller: [HandleError] public
Similar to this post IEnumerable model property in an ASP.NET MVC 3 Editor Template
I've got a model like this: public class ParentViewModel { public class ChildViewModel {
I have a PartialView strongly typed to a view model <%@ Control Language=C# Inherits=System.Web.Mvc.ViewUserControl<VATRateManager_MVC.Models.ViewModels.RateControlEditViewModel>
I have an Index.cshtml view: @model AttendenceModel @{ Layout = ~/Views/Shared/_Layout.cshtml; } @using (Html.BeginForm(VisOppsummering,

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.