Avatar of cdakz
Flag for United States of America asked on

Best Practice to process checked/selected items in an MVC View?

Using ASP.NET & MVC.

I'm going to have a list of checkboxes in my View (1 checkbox per row/record), then have a "Process All Checked Rows" button that will perform an action on all of the checked rows. I would like to process the checked rows in the Controller.

I'm worried that I'll come up with some "kludge" when there's an elegant way to do it.

ASP.NETjQueryWeb DevelopmentWeb Applications

Avatar of undefined
Last Comment

8/22/2022 - Mon

Who knows, maybe your "kludge" is more elegant than my solution.....

Anyway, I am doing something similar in an MVC 5 app.  I have a ViewModel to present a list of optional checkboxes.  In my Controller action I need to evaluate whether any were checked and, if so, which ones.

I pass in the checkbox as an array.  Let me know if you would like to see any additional code, e.g. from the ViewModel that constructs the selectedOpinions or the View itself.

OM Gang

        public ActionResult Create ([Bind(Include = "larARRecNum,larRecDate,artID,larUserAgency")] LegAuditReport legAuditReport, int[] selectedOpinions)
            //check to see if any report opinions were selected
            if (selectedOpinions != null)
                //construct empty list for report opinions
                legAuditReport.LegAuditReportOpinions = new List<LegAuditReportOpinion>();
                foreach (var opinion in selectedOpinions)
                    //add each report opinion selected
                    var opinionToAdd = new LegAuditReportOpinion();
                    opinionToAdd.aroID = opinion;
                    opinionToAdd.larID = legAuditReport.larID;
                    //use the userAgency value from the leg audit report record
                    opinionToAdd.laroUserAgency = legAuditReport.larUserAgency;

Open in new window


How are you populating the int[] selectedOpinions array from the View?
(Perhaps the crux of my question).


        @* checkboxes to select report opinions *@
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                            int cnt = 0;
                            List<GovCAP.ViewModels.AuditReportOpinionData> AuditReportOpinionTypes = ViewBag.AuditReportOpinionTypes;

                            foreach (var auditReportOpinion in AuditReportOpinionTypes)
                                if (cnt++ % 3 == 0)
                                @:<td class="opiniontable">
                                    <input type="checkbox"
                                        @(Html.Raw(auditReportOpinion.Selected ? "checked=\"checked\"" : "")) />
                                        @auditReportOpinion.OpinionID @: @auditReportOpinion.Description

Open in new window

Controller -- calls a method to construct the

                //construct empty entity object that we can pass to method to include releated records for report opinions
                var legAuditReport = new LegAuditReport();
                legAuditReport.LegAuditReportOpinions = new List<LegAuditReportOpinion>();

Open in new window

Controller method to construct the check box items and add to view bag

        // Constructs view model for report opinions for audit report passed as argument.
        private void PopulateAuditReportOpinionData(LegAuditReport legAuditReport)
            // Get all opinion types from db.
            var allOpinions = db.AuditReportOpinionTypes;

            // Get opinions selected for the audit report we're working with.
            var reportOpinions = new HashSet<int>(legAuditReport.LegAuditReportOpinions.Select(c => c.AuditReportOpinionType.aroID));

            var viewModel = new List<AuditReportOpinionData>();
            foreach (var opinion in allOpinions)
                // Construct the view model to include and opinion types from db and indicating which are selected for the report we're working with.
                viewModel.Add(new AuditReportOpinionData
                    OpinionID = opinion.aroID,
                    AuditReportID = legAuditReport.larID,
                    Description = opinion.aroDescription,
                    Selected = reportOpinions.Contains(opinion.aroID)
            ViewBag.AuditReportOpinionTypes = viewModel;

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck

....so, in the Controller GET action, I call a method to construct the list of opinions (checkbox items) for the specified report.  The method adds the list to the ViewBag.  In the View, the list (from the ViewBag) is iterated and html checkboxes are constructed for each, including whether or not each one is 'checked'.

I believe this is probably NOT elegant from a pure MVC standpoint as I'm adding logic to the View when it SHOULD only present html.  A "kludge", perhaps.  It does exactly what I want it to, however.
OM Gang

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.