1

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.

3
  • Are you sure your ListQuestionableVisits is invoked? Because you should set this as the target action in your Html.BeginForm() Commented Feb 23, 2012 at 3:48
  • Yes. I have tested this by putting in a breakpoint and it is calling the correct method in the controller. Commented Feb 23, 2012 at 3:55
  • @Marc - by default @Html.BeginForm() posts back to the same action, so you don't need any parameters in this case. Commented Feb 23, 2012 at 8:41

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@samack - hehe nice. Let this be a lesson, loops are evil. :) They are simply NEVER required in MVC views.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.