在我的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>
感謝您的幫助,
親切的問候
鮑勃
嗨尼古拉斯,謝謝你的回覆。我按照你的建議移動了Html.BeginForm。參數List模式仍爲空。我也試過FormCollection作爲參數,但也是null。我需要使用LABEL元素嗎? FORM元素如何知道要回傳哪些數據?關心Bob –
bobentelect
2011-01-10 11:08:27
@bobentelect - 您可以使用Html.HiddenFor – 2011-01-10 11:28:24