2010-11-17 184 views
1

我正在使用方法in this article來返回發佈到我的操作的對象列表。我的方法如下所示:當我期望值時IEnumerable爲空

// 
    // POST: /LeaveRequest/Create 
    [Authorize, HttpPost] 
    public ActionResult Create(LeaveRequest leaveRequest, IEnumerable<DayRequested> requestedDays) 
    { 
     return RedirectToAction("Index", lrRepository.GetLeaveRequests(472940821)); 
    } 

leaveRequest有我期望的數據。 requestedDays包含我期望的行數,但所有行在每個字段中都包含空值。我可能錯過了什麼想法?

這裏是查看代碼:

Create.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<EmployeePayroll.ViewModels.LeaveRequestViewModel>" %> 
<%@ Import Namespace="EmployeePayroll.Helpers"%> 
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">Create New Leave Request</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <h2>Create New Leave Request</h2> 
    <div><%= Html.ActionLink("Back to List", "Index") %></div> 
    <%= Html.Partial("RequestEditor", Model) %> 
    <div><%= Html.ActionLink("Back to List", "Index") %></div> 
</asp:Content> 

RequestEditor.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<EmployeePayroll.ViewModels.LeaveRequestViewModel>" %> 
<% using (Html.BeginForm()) {%> 
    <%= Html.ValidationSummary(true) %> 
    <fieldset> 
     <legend>Request Details</legend> 
     <table> 
      <tbody id="editorRows"> 
       <tr><th>Date</th><th>Time</th><th>Hours</th><th>Request Type</th><th></th></tr> 
       <% foreach (var item in Model.DaysRequested) 
        Html.RenderPartial("RequestedDayRow", new EmployeePayroll.ViewModels.LeaveRequestRow(item, Model.LeaveRequestType)); %> 
      </tbody> 
     </table> 
     <p><%= Html.ActionLink("Add Day", "BlankRequestedDayRow", null, new { id = "addItem" })%></p> 

     <p>Type your time to sign your request.</p> 
     <p><%= Html.LabelFor(model => model.LeaveRequest.EmployeeSignature) %>: <%= Html.TextBoxFor(model => model.LeaveRequest.EmployeeSignature, new { Class="required" })%></p> 
     <p><%= Html.LabelFor(model => model.LeaveRequest.EmployeeComment) %>: <%= Html.TextBoxFor(model => model.LeaveRequest.EmployeeComment) %></p> 
     <p><input type="submit" value="Submit Request" /></p> 
    </fieldset> 
<% } %> 

RequestedDayRow.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<EmployeePayroll.ViewModels.LeaveRequestRow>" %> 
<%@ Import Namespace="EmployeePayroll.Helpers"%> 

<tr class="editorRow"> 
    <% using (Html.BeginCollectionItem("requestedDays")) 
    { %> 
     <td><%= Html.TextBoxFor(model => model.DayRequested.DateOfLeave, new { Class = "datepicker", Maxlength = "10" })%></td> 
     <td><%= Html.TextBoxFor(model => model.DayRequested.TimeOfLeave, new { Class = "timedropdown", Maxlength = "8" })%></td> 
     <td><%= Html.TextBoxFor(model => model.DayRequested.HoursRequested, new { Class = "hoursdropdown", Maxlength = "4" })%></td> 
     <td><%= Html.DropDownListFor(model => model.DayRequested.RequestType, 
      new SelectList(Model.LeaveRequestType, "Value", "Text", Model.DayRequested.RequestType), "(Select)")%></td> 
     <td><a href="#" class="deleteRow"><img src="../../images/site_icons/16/69.png" title="Delete" alt="Delete" border="0" /></a></td> 
    <% } %> 
</tr> 

這裏是生成的表單:

<form method="post" action="/LeaveRequest/Create"> 
    <fieldset> 
     <legend>Request Details</legend> 
     <table> 
      <tbody id="editorRows"> 
       <tr><th>Date</th><th>Time</th><th>Hours</th><th>Request Type</th><th></th></tr> 
<tr class="editorRow"> 
    <input type="hidden" value="c43391a3-7fe4-4514-8b27-d00995d64848" autocomplete="off" name="requestedDays.index"> 

     <td><input type="text" value="11/17/2010" name="requestedDays[c43391a3-7fe4-4514-8b27-d00995d64848].DayRequested.DateOfLeave" id="requestedDays_c43391a3-7fe4-4514-8b27-d00995d64848__DayRequested_DateOfLeave" maxlength="10" class="datepicker"></td> 
     <td><input type="text" value="8:00 AM" name="requestedDays[c43391a3-7fe4-4514-8b27-d00995d64848].DayRequested.TimeOfLeave" id="requestedDays_c43391a3-7fe4-4514-8b27-d00995d64848__DayRequested_TimeOfLeave" maxlength="8" class="timedropdown"></td> 
     <td><input type="text" value="8" name="requestedDays[c43391a3-7fe4-4514-8b27-d00995d64848].DayRequested.HoursRequested" id="requestedDays_c43391a3-7fe4-4514-8b27-d00995d64848__DayRequested_HoursRequested" maxlength="4" class="hoursdropdown"></td> 
     <td><select name="requestedDays[c43391a3-7fe4-4514-8b27-d00995d64848].DayRequested.RequestType" id="requestedDays_c43391a3-7fe4-4514-8b27-d00995d64848__DayRequested_RequestType"><option value="">(Select)</option> 
</select></td> 
     <td><a class="deleteRow" href="#"><img border="0" alt="Delete" title="Delete" src="../../images/site_icons/16/69.png"></a></td> 
</tr> 
<tr class="editorRow"> 
    <input type="hidden" value="a24b74f6-2947-4ec5-a817-f938d6fe4e24" autocomplete="off" name="requestedDays.index"> 
     <td><input type="text" value="11/17/2010" name="requestedDays[a24b74f6-2947-4ec5-a817-f938d6fe4e24].DayRequested.DateOfLeave" id="requestedDays_a24b74f6-2947-4ec5-a817-f938d6fe4e24__DayRequested_DateOfLeave" maxlength="10" class="datepicker"></td> 
     <td><input type="text" value="8:00 AM" name="requestedDays[a24b74f6-2947-4ec5-a817-f938d6fe4e24].DayRequested.TimeOfLeave" id="requestedDays_a24b74f6-2947-4ec5-a817-f938d6fe4e24__DayRequested_TimeOfLeave" maxlength="8" class="timedropdown"></td> 
     <td><input type="text" value="8" name="requestedDays[a24b74f6-2947-4ec5-a817-f938d6fe4e24].DayRequested.HoursRequested" id="requestedDays_a24b74f6-2947-4ec5-a817-f938d6fe4e24__DayRequested_HoursRequested" maxlength="4" class="hoursdropdown"></td> 
     <td><select name="requestedDays[a24b74f6-2947-4ec5-a817-f938d6fe4e24].DayRequested.RequestType" id="requestedDays_a24b74f6-2947-4ec5-a817-f938d6fe4e24__DayRequested_RequestType"><option value="">(Select)</option> 
</select></td> 
     <td><a class="deleteRow" href="#"><img border="0" alt="Delete" title="Delete" src="../../images/site_icons/16/69.png"></a></td> 
</tr> 
</tbody> 
    </table> 
    <p><a id="addItem" href="/LeaveRequest/BlankRequestedDayRow">Add Day</a></p> 

    <p>Type your time to sign your request.</p> 
    <p><label for="LeaveRequest_EmployeeSignature">Employee Signature</label>: <input type="text" value="" name="LeaveRequest.EmployeeSignature" id="LeaveRequest_EmployeeSignature" class="required"></p> 
    <p><label for="LeaveRequest_EmployeeComment">Employee Comment</label>: <input type="text" value="" name="LeaveRequest.EmployeeComment" id="LeaveRequest_EmployeeComment"></p> 
    <p><input type="submit" value="Submit Request"></p> 
</fieldset> 
</form> 

表單發送以下信息:

Parametersapplication/x-www-form-urlencoded 
LeaveRequest.EmployeeComm... Comment 
LeaveRequest.EmployeeSign... Michael Wills 
requestedDays.index 549a7c9a-9c7d-4032-a1cd-6bfda92bf1f2 
requestedDays.index 2838b025-d971-4d98-a081-5ea0c559aebb 
requestedDays[2838b025-d9... 11/17/2010 
requestedDays[2838b025-d9... 8 
requestedDays[2838b025-d9... 1 
requestedDays[2838b025-d9... 8:00 AM 
requestedDays[549a7c9a-9c... 11/17/2010 
requestedDays[549a7c9a-9c... 8 
requestedDays[549a7c9a-9c... 1 
requestedDays[549a7c9a-9c... 8:00 AM 
Source 
Content-Type: application/x-www-form-urlencoded 
Content-Length: 907 
requestedDays.index=549a7c9a-9c7d-4032-a1cd-6bfda92bf1f2&requestedDays%5B549a7c9a-9c7d-4032-a1cd-6bfda92bf1f2%5D.DayRequested.DateOfLeave=11%2F17%2F2010&requestedDays%5B549a7c9a-9c7d-4032-a1cd-6bfda92bf1f2%5D.DayRequested.TimeOfLeave=8%3A00+AM&requestedDays%5B549a7c9a-9c7d-4032-a1cd-6bfda92bf1f2%5D.DayRequested.HoursRequested=8&requestedDays%5B549a7c9a-9c7d-4032-a1cd-6bfda92bf1f2%5D.DayRequested.RequestType=1&requestedDays.index=2838b025-d971-4d98-a081-5ea0c559aebb&requestedDays%5B2838b025-d971-4d98-a081-5ea0c559aebb%5D.DayRequested.DateOfLeave=11%2F17%2F2010&requestedDays%5B2838b025-d971-4d98-a081-5ea0c559aebb%5D.DayRequested.TimeOfLeave=8%3A00+AM&requestedDays%5B2838b025-d971-4d98-a081-5ea0c559aebb%5D.DayRequested.HoursRequested=8&requestedDays%5B2838b025-d971-4d98-a081-5ea0c559aebb%5D.DayRequested.RequestType=1&LeaveRequest.EmployeeSignature=Michael+Wills&LeaveRequest.EmployeeComment=Comment 
+0

我知道我沒有對我的方法中的數據做任何事情。在繼續之前,我正在驗證我得到的結果。 – 2010-11-17 21:46:14

+0

你不應該再需要'.Index'隱藏表單域了。事實上,我認爲它實際上在ASP.NET MVC 2中打破了。試着把它解決掉。 – 2010-11-17 21:50:26

+0

@丹·阿特金森 - 讓事情變得更糟,對象現在是'空' – 2010-11-17 21:55:56

回答

0

您好像需要從隱藏的表單字段名稱中刪除DayRequested

例如:

... I removed some of the attributes because the only thing that's changed here is the name attribute. 
<input type="hidden" name="requestedDays[c43391a3-7fe4-4514-8b27-d00995d64848].DateOfLeave" value="11/17/2010" /> 
<input type="hidden" name="requestedDays[c43391a3-7fe4-4514-8b27-d00995d64848].TimeOfLeave" value="8:00 AM" /> 
<input type="hidden" name="requestedDays[c43391a3-7fe4-4514-8b27-d00995d64848].HoursRequested" value="8" /> 
... 

如問題的意見討論,似乎奇怪的是,ASP.NET MVC會產生你給的來源,因爲這意味着每個RequestedDay對象有一個名爲RequestedDay的一個屬性。

您綁定的對象名爲requestedDays,並且該GUID表示您要綁定的集合中的哪個位置(在此處陳述明顯,我知道!)。因此,聲明對象名稱(DayRequested)沒有任何意義,因爲模型聯編程序應該已經知道這一點,只需知道它正在處理的那個類的哪個屬性。

+0

好吧,我猜我抄的代碼是錯的。我會這個工作。我手動創建了名稱和ID,現在它可以工作。謝謝! – 2010-11-17 22:38:17

0

看到這個帖子從Haacked - Model Binding To A List 它可以幫助你

+0

從我所看到的,我所做的(最終)與他們所做的一樣。 – 2010-11-17 21:59:05