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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:38:00+00:00 2026-06-08T03:38:00+00:00

I’m trying to implement a permission screen in which a user can be given

  • 0

I’m trying to implement a permission screen in which a user can be given a particular permission on a particular screen. For this I’m generating a collection of Checkboxfor, bound to a collection of bool properties. But when I submit the form, I’m either getting all bool properties true or all false, depending on whether I initialized these properties as true or false in the viewmodel constructor.

Here is the code for the ViewModel:

Approach I:

public class MyViewModel
{
    public MyModel Model { get; set; }        

    public IEnumerable<ScreenType> Screens { get; set; }
    public IEnumerable<SecurityType> SecurityTypes { get; set; }
    public List<PermissionType> Permissions { get; set; }        

    public MyViewModel()
    {
        LoadScreens();
        LoadSecurityTypes();
        LoadPermissions();
    }

    public void LoadPermissions()
    {
        Permissions = new List<PermissionType>();

        foreach (var screen in Screens)
        {
            foreach (var securityType in SecurityTypes)
            {
                Permissions.Add(
                    new PermissionType
                    {
                        PermissionId= Guid.NewGuid(),
                        ScreenId= screen.Id,
                        SecurityId = securityType.Id,
                        IsAllowed = false
                    });
            }
        }
    }    
}

Approach II

public class MyViewModel
{
    public MyModel Model { get; set; }        

    public IEnumerable<ScreenType> Screens { get; set; }
    public IEnumerable<SecurityType> SecurityTypes { get; set; }
    public List<bool> AllowedList { get; set; }        

    public MyViewModel()
    {
        LoadScreens();
        LoadSecurityTypes();
        LoadPermissions();
    }

    public void LoadPermissions()
    {
        AllowedList = new List<bool>();

        foreach (var form in Forms)
        {
            foreach (var security in Securities)
            {
                AllowedList.Add(false);
            }
        }
    }    
}

Here is the code my the view:

Approach I:

    @using (Ajax.BeginForm("Create", "Role", null, new AjaxOptions { UpdateTargetId = "addStatus", InsertionMode = InsertionMode.Replace, OnSuccess = "onFormPostSuccess" }, new { @id = "AddForm" }))
    {  
        <div>
            <span><label>Screen</label></span>
            @foreach (var security in Model.SecurityTypes)
            { 
                <span><label>@security.Name</label></span>                    
            }
            <br />
            @foreach (var screen in Model.Screens)
            {
                <span>@screen.Name</span>
                foreach (var security in Model.SecurityTypes)
                { 
                    <span>@Html.CheckBoxFor(m => m.Permissions.Where(s => s.SecurityId == security.Id && s.ScreenId == screen.Id).Single().IsAllowed, new { @id = HtmlHelper.GenerateIdFromName("Create" + screen.Name + security.Name) })</span>                   
                }
                <br />
            }
        </div>
        <div>
            <span>
                <input type="image" src="/content/images/submit_button.png" value="submit" />
            </span> 
        </div>
        <div>
            <span id="addStatus" class="submitStatus"></span>
        </div>
    }

Approach II:

 @using (Ajax.BeginForm("Create", "Role", null, new AjaxOptions { UpdateTargetId = "addStatus", InsertionMode = InsertionMode.Replace, OnSuccess = "onFormPostSuccess" }, new { @id = "AddForm" }))
    {  
        <div>
            <span><label>Screen</label></span>
            @foreach (var security in Model.SecurityTypes)
            { 
                <span><label>@security.Name</label></span>                    
            }
            <br />
            @foreach (int i=0; i < Model.Screens.Count(); i++)
            {
                <span>@Model.Screens.ElementAt(i).Name</span>
                foreach (int j=0; j<Model.SecurityTypes.Count(); j++)
                { 
                    @* The index 5*i+j is because I have 5 security types *@ 
                    <span>@Html.CheckBoxFor(Model.AllowedList[5*i+j], new { @id = HtmlHelper.GenerateIdFromName("Create" + @Model.Screens.ElementAt(i).Name + @Model.SecurityTypes.ElementAt(j).Name) })</span>                   
                }
                <br />
            }
        </div>
        <div>
            <span>
                <input type="image" src="/content/images/submit_button.png" value="submit" />
            </span> 
        </div>
        <div>
            <span id="addStatus" class="submitStatus"></span>
        </div>
    }

Here’s the code for the Create actionmethod in Controller:

    [Authorize]
    [HttpPost]
    public JsonResult Create(MyViewModel viewModel)
    {   
        if ( ModelState.IsValid)
        {                

            if (service.AddRole(viewModel))
            {                    
                return Json("Role Added !");
            }
            return Json("Role exists !");
        }
        return Json("Please correct errors");
    }

When I check the viewModel in the Create actionmethod, all the IsAllowed properties are false. As were initialized in the viewmodel contructor. There is no effect of checking/unchecking Checkboxes from the view.

Am I missing something?

  • 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-08T03:38:03+00:00Added an answer on June 8, 2026 at 3:38 am

    Strongly typed HTML helpers such as the CheckBoxFor work only with simple expressions as first argument (property and array index access at most). The following expression is something completely out of the capacities of this helper:

    m => m.Permissions.Where(s => s.SecurityId == security.Id && s.ScreenId == screen.Id).Single().IsAllowed
    

    I would recommend you to use a real view model and perform this at your controller level when mapping between your domain model and the view model.


    UPDATE:

    Apparently my answer about view models wasn’t clear enough, so let me try to exemplify what I mean by view models.

    So we start by defining our view models that will be required for implementing the required logic for this view:

    public class CreateViewModel
    {
        public IEnumerable<ScreenViewModel> Screens { get; set; }
    }
    
    public class ShowCreateViewModel: CreateViewModel
    {
        public IEnumerable<SecurityTypeViewModel> SecurityTypes { get; set; }
    }
    
    public class ScreenViewModel
    {
        public string ScreenName { get; set; }
        [HiddenInput(DisplayValue = false)]
        public int ScreenId { get; set; }
        public IEnumerable<SecurityTypeViewModel> SecurityTypes { get; set; }
    }
    
    public class SecurityTypeViewModel
    {
        public string SecurityName { get; set; }
        [HiddenInput(DisplayValue = false)]
        public int SecurityTypeId { get; set; }
        public bool IsAllowed { get; set; }
    }
    

    Then we could have a controller action that will take care of fetching the domain models from a repository or something and map to the view models:

    public class HomeController : Controller
    {
        public ActionResult Create()
        {
            // The information will obviously come from a domain model that 
            // we map to a view model, but for the simplicity of the answer
            // I am hardcoding the values here
            var securityTypes = new[]
            {
                new SecurityTypeViewModel { SecurityTypeId = 1, SecurityName = "security 1" },
                new SecurityTypeViewModel { SecurityTypeId = 2, SecurityName = "security 2" },
                new SecurityTypeViewModel { SecurityTypeId = 3, SecurityName = "security 3" },
            };
    
            // The information will obviously come from a domain model that 
            // we map to a view model, but for the simplicity of the answer
            // I am hardcoding the values here
            return View(new ShowCreateViewModel
            {
                SecurityTypes = securityTypes,
                Screens = new[]
                {
                    new ScreenViewModel 
                    {
                        ScreenId = 1,
                        ScreenName = "Screen 1",
                        SecurityTypes = securityTypes
                    },
                    new ScreenViewModel 
                    {
                        ScreenId = 2,
                        ScreenName = "Screen 2",
                        SecurityTypes = securityTypes
                    },
                }
            });
        }
    
        [HttpPost]
        public ActionResult Create(CreateViewModel model)
        {
            // The view model passed here will contain all the necessary information
            // for us to be able to perform the actual Save: 
            // a list of the screen ids along with a list of the selected permission ids
    
            return Content(
                "Thank you for selecting the following allowed permissions:<br/>" + 
                string.Join("<br/>", model.Screens.Select(s => string.Format(
                    "screen id: {0}, permission ids: {1}", 
                    s.ScreenId, 
                    string.Join(",", s.SecurityTypes.Where(st => st.IsAllowed).Select(st => st.SecurityTypeId))
                )))
            );
        }
    }
    

    and now what’s left is to define the view and the corresponding editor/display templates.

    Let’s start with the main view (~/Views/Home/Create.cshtml):

    @model ShowCreateViewModel
    
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
    
    @using (Ajax.BeginForm("Create", "Home", null, new AjaxOptions { UpdateTargetId = "addStatus", InsertionMode = InsertionMode.Replace, OnSuccess = "onFormPostSuccess" }, new { @id = "AddForm" }))
    {  
        <table>
            <thead>
                <tr>
                    <th>Screen/Security type</th>
                    @Html.DisplayFor(x => x.SecurityTypes)
                </tr>
            </thead>
            <tbody>
                @Html.EditorFor(x => x.Screens)
            </tbody>
        </table>    
    
        <div>
            <input type="submit" value="submit" />
        </div>
    
        <div id="addStatus" class="submitStatus"></div>
    }
    

    Next we have an editor template for the ScreenViewModel model (~/Views/Shared/EditorTemplates/ScreenViewModel.cshtml):

    @model ScreenViewModel
    <tr>
        <td>
            @Html.DisplayFor(x => x.ScreenName)
            @Html.EditorFor(x => x.ScreenId)
        </td>
        @Html.EditorFor(x => x.SecurityTypes)
    </tr>
    

    Then the editor template for the SecurityTypeViewModel model (~/Views/Shared/EditorTemplates/SecurityTypeViewModel.cshtml):

    @model SecurityTypeViewModel
    <td>
        @Html.CheckBoxFor(x => x.IsAllowed)
        @Html.EditorFor(x => x.SecurityTypeId)
    </td>
    

    And finally the display template for the SecurityTypeViewModel model (~/Views/Shared/DisplayTemplates/SecurityTypeViewModel.cshtml):

    @model SecurityTypeViewModel
    <th>
        @Html.DisplayFor(x => x.SecurityName)
    </th>
    

    And that’s pretty much it:

    enter image description here

    I have left for you the mapping between your actual domain models and the view models defined here.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

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.