samack
samack

Reputation: 815

MVC3 Checkbox passing values from view back to model

I have a view model which is just

public class Visits
{
    public List<Visit> visits { get; set; }
}

In my visit model I have a

public bool ValidVisit { get; set; }

I am able to pass everything to my view ok and render all of the visits on the view. The view looks like

@model TheWallSite.ObjectModels.Visits

@{
    ViewBag.Title = "Potential invalid visits!";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
       <fieldset>
        <table>
            <tr><th>Check In/Out Time</th><th>Visit Type</th><th>In/Out</th><th>IP</th><th>SSO ID</th><th>Valid Visit</th></tr>

            @foreach (var item in Model.visits)
            {
                <tr>
                    <td>@Html.DisplayFor(model => item.InOutTime)</td>
                    <td>@Html.DisplayFor(model => item.VisitType)</td>
                    <td>@Html.DisplayFor(model => item.VisitName)</td>
                    <td>@Html.DisplayFor(model => item.IP)</td>
                    <td>@Html.DisplayFor(model => item.SSO)</td>
                    <td>@Html.EditorFor(model => item.ValidVisit)</td>
                </tr>
            }
        </table>
        <input type="submit" value="Submit" />
       </fieldset>
}

The problem I am having is I want the end user to be able to check/uncheck the ValidVisit and then pass these back to the controller and make the correct changes in my database.. I am having a heck of a time figuring out how to do this. Any suggestions? My [HttpPost] controller signature is

public ActionResult ListQuestionableVisits(Visits model, FormCollection forms)

but nothing seems to be getting back to the controller.

Upvotes: 1

Views: 1487

Answers (1)

RPM1984
RPM1984

Reputation: 73123

It would be the model-binding not kicking in, probably due to the loop.

I know i know, it should work, but do it the right way, and it has a better chance of working.

Try using editor templates instead.

/EditorTemplates/Visit.cshtml

@model TheWallSite.ObjectModels.Visit
<tr><td>@Html.DisplayFor(model => model.InOutTime)</td></tr>
<tr><td>@Html.DisplayFor(model => model.VisitType)</td></tr>
<tr><td>@Html.DisplayFor(model => model.VisitName)</td></tr>
<tr><td>@Html.DisplayFor(model => model.IP)</td></tr>
<tr><td>@Html.DisplayFor(model => model.SSO)</td></tr>
<tr><td>@Html.EditorFor(model => model.ValidVisit)</td></tr>

Main View:

@model TheWallSite.ObjectModels.Visits

@{
    ViewBag.Title = "Potential invalid visits!";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
       <fieldset>
        <table>
            <tr>
               <th>Check In/Out Time</th>
               <th>Visit Type</th>
               <th>In/Out</th>
               <th>IP</th>
               <th>SSO ID</th>
               <th>Valid Visit</th>
            </tr>                
            @Html.EditorFor(model => model.Visits)
        </table>
        <input type="submit" value="Submit" />
       </fieldset>
}

Also, if that's your complete view, you don't need the FormCollection parameter in the action, unless there is a hidden field/some other magic field i'm not seeing.

Upvotes: 2

Related Questions