Link to home
Start Free TrialLog in
Avatar of cdakz
cdakzFlag 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.

Suggestions?
Avatar of omgang
omgang
Flag of United States of America image

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

        [HttpPost]
        [ValidateAntiForgeryToken]
        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;
                    legAuditReport.LegAuditReportOpinions.Add(opinionToAdd);
                }
            }

Open in new window

Avatar of cdakz

ASKER

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

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

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

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>();
                PopulateAuditReportOpinionData(legAuditReport);

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

....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
ASKER CERTIFIED SOLUTION
Avatar of omgang
omgang
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial