2014-03-12 50 views
0

我有一個IList對象,使用ViewModel傳遞給我的視圖。在MVC視圖中使用複選框,並將選中的狀態傳遞迴控制器進行處理

 [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名單的項目,但除複選框狀態以外的所有內容都爲空。

任何人都可以幫我解釋爲什麼和如何解決這個問題,所以我看到所有的模型數據?

Jason。

回答

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

在每個DisplayFor話費,加入@Html.HiddenFor(x=>x[i].Property)調用添加含有相同值的隱藏字段。

隱藏字段值將在表單中發佈,並在數據到達控制器時綁定到您的模型。

DisplayFor將只是一個標籤添加到輸出(除非被覆蓋與顯示器模板模型),標籤值不要被形式發佈。

1

您不回傳值。 Html.DisplayFor不會生成輸入標籤,並且只有表單內的輸入被髮回到服務器。您將不得不爲這些屬性使用Html.TexBoxForHtml.HiddenFor方法。

相關問題