2011-01-10 61 views
0

在我的ASP .Net MVC 2應用程序中,用戶顯示了項目列表,並且必須在列表持久保存到數據庫之前單擊確認。如何在HttpPost方法中獲得確認列表(IEnumerable)?

當用戶單擊確認時,在HttpPost方法中參數爲空。一切正常,直到HttpPost方法被調用。此時,必須保持的列表爲空。

如何獲得確認值?

我試過在HttpGet方法中使用TempData,但TempData在HttpPost方法中也是null。

這裏是控制器代碼。

public ActionResult Confirm() 
    { 
     List<ConfirmVehicleModel> vehicles = GetAllVehicles(); 
     return View(vehicles); 
    } 

    [HttpPost] 
    public ActionResult Confirm(List<ConfirmVehicleModel> model) 
    { 

     //model is null, why ? 

     UploadVehiclesModelService service = new Models.UploadVehiclesModelService(); 
     service.StoreVehicles(model, User.Identity.Name); 

     return RedirectToAction("Index", "UploadVehicles"); 
    } 

這裏是確認的觀點:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<RM.Application.Models.ConfirmVehicleModel>>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Confirm 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
<h2> 
    Confirm that the vehicles below should be added.</h2> 
<table> 
    <tr> 
     <th> 
      ReferenceType 
     </th> 
     <th> 
      ReferenceName 
     </th> 
    </tr> 
    <% foreach (var item in Model) 
     { %> 
    <tr> 
     <td> 
      <%= Html.Encode(item.ReferenceType) %> 
     </td> 
     <td> 
      <%= Html.Encode(item.ReferenceName) %> 
     </td> 
    </tr> 
    <% } %> 
</table> 
<div> 
    <% using (Html.BeginForm()) 
     { %> 
    <input type="submit" value="Confirm" /> 
    | 
    <%= Html.ActionLink("Back to upload form", "Index") %> 
    <% } %> 
</div> 
</asp:Content> 

感謝您的幫助,

親切的問候

鮑勃

回答

1

你HTML.BeginForm()是出把它放在你希望傳遞的值的周圍。

嘗試:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
<h2>Confirm that the vehicles below should be added.</h2> 
<% using (Html.BeginForm()) 
    { %> 
    <table> 
     <tr> 
      <th> 
       ReferenceType 
      </th> 
      <th> 
       ReferenceName 
      </th> 
     </tr> 
     <% foreach (var item in Model) 
      { %> 
     <tr> 
      <td> 
       <%= Html.Encode(item.ReferenceType) %> 
       <%= Html.HiddenFor(model => item.ReferenceType)%> 
      </td> 
      <td> 
       <%= Html.Encode(item.ReferenceName) %> 
       <%= Html.HiddenFor(model => item.ReferenceName)%> 
      </td> 
     </tr> 
     <% } %> 
    </table> 
<div> 
    <input type="submit" value="Confirm" /> 
    | 
    <%= Html.ActionLink("Back to upload form", "Index") %> 
    <% } %> 
</div> 

+0

嗨尼古拉斯,謝謝你的回覆。我按照你的建議移動了Html.BeginForm。參數List 模式仍爲空。我也試過FormCollection作爲參數,但也是null。我需要使用LABEL元素嗎? FORM元素如何知道要回傳哪些數據?關心Bob – bobentelect 2011-01-10 11:08:27

+0

@bobentelect - 您可以使用Html.HiddenFor – 2011-01-10 11:28:24

0

尼古拉斯諮詢幫助。我用for循環替換了foreach循環,並使用Html.HiddenFor

<% for (int i = 0; i < Model.Count(); i++) 
      { %> 
     <%= Html.HiddenFor(model=>model[i].ReferenceType) %> 
     <%= Html.HiddenFor(model=>model[i].ReferenceName) %> 
     <tr> 
      <td> 
       <%= Html.Encode(Model[i].ReferenceType) %> 
      </td> 
      <td> 
       <%= Html.Encode(Model[i].ReferenceName) %> 
      </td> 
     </tr> 
     <% } %> 

我也將視圖的第一行改爲(以前是IEnumerable,現在是List)。

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<List<RM.Application.Models.ConfirmVehicleModel>>" %>\\ 

HttpPost方法的List<ConfirmVehicleModel>模型參數現在已填充。