【问题标题】:Use checkboxes in MVC view and pass checked state back to controller for processing在 MVC 视图中使用复选框并将选中状态传递回控制器进行处理
【发布时间】:2014-04-16 07:10:26
【问题描述】:

我有一个 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 类型的列表项,但是除了复选框状态之外的所有内容都是空的。

谁能帮助我解决这个问题的原因和方法,以便我查看所有模型数据?

杰森。

【问题讨论】:

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


    【解决方案1】:

    这是因为您的表单仅包含复选框的输入字段。 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>
        }
    

    【讨论】:

      【解决方案2】:

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

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

      DisplayFor 只会在输出中添加标签(除非使用显示模板覆盖模型),标签值不会被表单发布。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-06-06
          • 1970-01-01
          • 2012-12-31
          • 2016-08-19
          • 2013-06-06
          • 1970-01-01
          • 1970-01-01
          • 2017-12-31
          相关资源
          最近更新 更多