Binding to a collection of CheckboxFor

后端 未结 1 776
野性不改
野性不改 2021-01-15 18:22

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 Checkboxfo

1条回答
  •  -上瘾入骨i
    2021-01-15 18:41

    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 Screens { get; set; }
    }
    
    public class ShowCreateViewModel: CreateViewModel
    {
        public IEnumerable SecurityTypes { get; set; }
    }
    
    public class ScreenViewModel
    {
        public string ScreenName { get; set; }
        [HiddenInput(DisplayValue = false)]
        public int ScreenId { get; set; }
        public IEnumerable 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:
    " + string.Join("
    ", 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
    
    
    
    @using (Ajax.BeginForm("Create", "Home", null, new AjaxOptions { UpdateTargetId = "addStatus", InsertionMode = InsertionMode.Replace, OnSuccess = "onFormPostSuccess" }, new { @id = "AddForm" }))
    {  
        
                    @Html.DisplayFor(x => x.SecurityTypes)
                
                @Html.EditorFor(x => x.Screens)
            
    Screen/Security type
    }

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

    @model ScreenViewModel
    
        
            @Html.DisplayFor(x => x.ScreenName)
            @Html.EditorFor(x => x.ScreenId)
        
        @Html.EditorFor(x => x.SecurityTypes)
    
    

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

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

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

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

    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 讨论(0)
提交回复
热议问题