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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:04:17+00:00 2026-05-31T06:04:17+00:00

Model : public IEnumerable<Role> Roles { get; set; } Index : Roles = securityServiceClient.GetAllRoles()

  • 0

Model :

public IEnumerable<Role> Roles { get; set; }

Index :

Roles = securityServiceClient.GetAllRoles()

View :

@foreach (var role in Model.Roles)
            {
                <tr>
                    @if (role.Name == "User")
                    {
                        <td><input type="checkbox" checked="checked"/></td>
                    }
                    else
                    {
                        <td><input type="checkbox"/></td>
                    }
                    <td>@Html.Label(role.Name)</td>
                </tr>
            }

 [HttpPost]
CreateSomething :

How can I get the selected checkbox(s) from the view?

  • 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-31T06:04:18+00:00Added an answer on May 31, 2026 at 6:04 am

    You must give your checkboxes names:

    @if (role.Name == "User")
    {
        <td><input type="checkbox" checked="checked" name="roles"/></td>
    }
    else
    {
        <td><input type="checkbox" name="roles"/></td>
    }
    

    and then:

    [HttpPost]
    public ActionResult Index(IEnumerable<string> roles)
    {
        ...    
    }
    

    Also you should be using view models, strongly typed views and helpers and not hardcode checkboxes as you did in your veiws.

    Here’s what I mean:

    Model:

    public class MyViewModel
    {
        public RoleViewModel[] Roles { get; set; }
    }
    
    public class RoleViewModel
    {
        public string RoleName { get; set; }
        public bool IsSelected { get; set; }
    }
    

    and then:

    public class HomeController: Controller
    {
        public ActionResult Index()
        { 
            var roles = securityServiceClient.GetAllRoles().Select(r => new RoleViewModel
            {
                RoleName = r.Name
            });
    
            var model = new MyViewModel
            {
                Roles = roles
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(IEnumerable<RolesViewModel> roles)
        {
            ...
        }
    }
    

    and in the view:

    @model MyViewModel
    @using (Html.BeginForm())
    {
        <table>
            <thead>
                <tr>
                    <th>role</th>
                </tr>
            </thead>
            <tbody>
                @for (var i = 0; i < Model.Roles.Length; i++)
                {
                    <tr>
                        <td>
                            @Html.CheckBoxFor(x => x.Roles[i].IsSelected)
                            @Html.LabelFor(x => x.Roles[i].IsSelected, Model.Roles[i].RoleName)
                            @Html.HiddenFor(x => x.Roles[i].RoleName)
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Model public class PdfPage { public IEnumerable<PdfPhoto> Photos { get; set; } } public
My model: public class Father { Set<Son> sons = new HashSet<Son>(); String name =
Here's my model public class Movie { public int MovieID { get; set; }
Consider the following model: public class BandProfileModel { public BandModel Band { get; set;
I have a model: public class MyModel { int id { get; set; }
In Spring MVC when placing an object in the view model like so: public
Here is the code: public IEnumerable<UserSummary> getUserSummaryList() { var db = new entityContext(); List<UserSummary>
So I have a page to edit employees. Here is my view model: public
I use a foreach in my View to loop over my strongly-typed Model and
I have the following classes in my Model: public abstract class Entity : IEntity

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.