如何在HttpPost方法中获取确认列表(IEnumerable)?

时间:2011-01-10 08:44:46

标签: asp.net asp.net-mvc

在我的ASP .Net MVC 2应用程序中,向用户显示项目列表,并且必须在列表持久保存到数据库之前单击确认。

当用户单击confirm时,在HttpPost方法中该参数为null。一切都有效,直到调用HttpPost方法。此时,必须保留的列表为空。

如何获得确认值?

我尝试在HttpGet方法中使用TempData,但在HttpPost方法中TempData也为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>

感谢您的帮助,

亲切的问候

鲍勃

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>>" %>\\

现在填充List<ConfirmVehicleModel>方法的HttpPost模型参数。