在MVC视图中使用复选框,并将检查状态传递回控制器进行处理

时间:2014-03-12 09:07:58

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-5

我有一个IList的对象,我使用ViewModel传递给我的视图。

        [HttpGet]
    public ActionResult Reminder()
    {
        using(var db = new SalonContext())
        {
            var tomorrow = DateTime.Today.AddDays(1);
            var homeSalon = GetHomeSalonId();
            var myAppointments = db.Appointments.Include(c => c.Customer).Include(c => c.Treatment).Include(c => c.Stylist).Include(c => c.Salon).Where(c => c.SalonId == homeSalon).ToList();
            var tomorrowsAppointments = myAppointments.Where(a => a.Start.Date == tomorrow).Select(rem => new FutureAppointment
                {
                    AppointmentId = rem.AppointmentId,
                    Name = rem.Customer.Title + " " +rem.Customer.FirstName + " " + rem.Customer.Surname,
                    AppointmentTime = rem.Start,
                    Salon = rem.Salon.Name,
                    Stylist = rem.Stylist.FirstName,
                    Treatment = rem.Treatment.Name,
                    Email = rem.Customer.EMail,
                    SendReminder = true
                }).ToList();

            return View(tomorrowsAppointments);
        }
    }

视图使用以下代码呈现带有复选框的列表:

@model IList<MySalonOrganiser.Models.FutureAppointment>

@{
    ViewBag.Title = "Email Reminders";
}

<h2>@ViewBag.Title</h2>

@using(Html.BeginForm("Reminder", "Appointment", FormMethod.Post))
{
    @Html.AntiForgeryToken()

    <table class="table">
        <tr>
            <th>
                Send Reminder
            </th>
            <th>
                Name
            </th>
            <th>
                Appointment Time
            </th>
            <th>
                Salon
            </th>
            <th>
                Treatment
            </th>
            <th>
                Stylist
            </th>
            <th>
                EMail
            </th>
            <th></th>
        </tr>

        @for (var i =0; i < Model.Count; i++)
        {
            <tr>
                <td>
                    @Html.CheckBoxFor(x => x[i].SendReminder)
                </td>
                <td>
                    @Html.DisplayFor(x => x[i].Name)
                </td>
                <td>
                    @Html.DisplayFor(x => x[i].AppointmentTime)
                </td>
                <td>
                    @Html.DisplayFor(x => x[i].Salon)
                </td>
                <td>
                    @Html.DisplayFor(x => x[i].Treatment)
                </td>
                <td>
                    @Html.DisplayFor(x => x[i].Stylist)
                </td>
                <td>
                    @Html.DisplayFor(x => x[i].Email)
                </td>
            </tr>
        }
    </table>
     <div>
        <input type="Submit" value="Send Reminders" />
    </div>

}

当我将视图发布回控制器时,我在返回的模型中看到了FutureAppointment类型的列表项,但除了复选框状态之外的所有内容都为null。

任何人都可以帮我解决为什么以及如何解决这个问题,以便查看所有模型数据?

杰森。

3 个答案:

答案 0 :(得分:2)

这是因为您的表单只包含复选框的输入字段。 DisplayFor助手只会创建文本或标签或显示其他字段值的任何内容。您可以通过为它们添加隐藏输入来解决此问题:

@for (var i =0; i < Model.Count; i++)
    {
        <tr>
            <td>
                @Html.CheckBoxFor(x => x[i].SendReminder)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].Name)
                @Html.HiddenFor(x => x[i].Name)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].AppointmentTime)
                @Html.HiddenFor(x => x[i].AppointmentTime)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].Salon)
                @Html.HiddenFor(x => x[i].Salon)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].Treatment)
                @Html.HiddenFor(x => x[i].Treatment)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].Stylist)
                @Html.HiddenFor(x => x[i].Stylist)
            </td>
            <td>
                @Html.DisplayFor(x => x[i].Email)
                @Html.HiddenFor(x => x[i].Email)
            </td>
        </tr>
    }

答案 1 :(得分:1)

在每个DisplayFor来电时,添加@Html.HiddenFor(x=>x[i].Property)来调用以添加包含相同值的隐藏字段。

隐藏字段值将由表单发布,并在数据到达您的控制器时绑定到您的模型。

DisplayFor只会在输出中添加标签(除非为具有显示模板的模型重写),标签值不会被表单发布。

答案 2 :(得分:1)

您没有回发这些值。 Html.DisplayFor不生成输入标记,只有表单内的输入才会发回服务器。您必须对这些属性使用Html.TexBoxForHtml.HiddenFor方法。