【问题标题】:How to get confirmed list (IEnumerable) in HttpPost method?如何在 HttpPost 方法中获取确认列表(IEnumerable)?
【发布时间】:2011-06-06 09:49:30
【问题描述】:

在我的 ASP .Net MVC 2 应用程序中,用户会看到一个项目列表,并且必须在列表被持久化到数据库之前单击确认。

当用户点击确认时,在 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>

感谢您的帮助,

亲切的问候

鲍勃

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案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>
    

    【讨论】:

    • 您好 Nicholas,感谢您的回复。我按照您的建议移动了 Html.BeginForm 。参数 List 模式仍然为空。我也尝试过 FormCollection 作为参数,但这也是空的。我需要使用 LABEL 元素吗? FORM 元素如何知道要回发哪些数据?问候鲍勃
    • @bobentelect - 您可以使用 Html.HiddenFor 将表单中的值“嵌入”为隐藏值
    【解决方案2】:

    尼古拉斯的建议有所帮助。我用 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&lt;ConfirmVehicleModel&gt; 模型参数现已填充。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-10
      • 2018-11-23
      • 1970-01-01
      • 2017-08-16
      • 1970-01-01
      • 2020-01-18
      • 2022-11-19
      相关资源
      最近更新 更多