开发者

How to get confirmed list (IEnumerable) in HttpPost method?

开发者 https://www.devze.com 2023-02-03 21:52 出处:网络
in my ASP .Net MVC 2 application the user is shown a list of items and must click confirm before the list is persisted to the database.

in my ASP .Net MVC 2 application the user is shown a list of items and must click confirm before the list is persisted to the database.

When the user clicks confirm, in the HttpPost method the parameter is null. Everything works until the HttpPost method is called. At this point the list that must be persisted is null.

How do I get the confirmed values ?

I've tried using TempData in the HttpGet method but TempData is also null in the HttpPost method.

Here is the controller code.

    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");
    }

And here is the Confirm view:

<%@ 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.En开发者_JAVA技巧code(item.ReferenceName) %>
        </td>
    </tr>
    <% } %>
</table>
 <div>
    <% using (Html.BeginForm())
       { %>
    <input type="submit" value="Confirm" />
    |
    <%= Html.ActionLink("Back to upload form", "Index") %>
    <% } %>
</div>
</asp:Content>

Thanks for any help,

Kind regards

Bob


Your HTML.BeginForm() is out of place it should surround the values you wish to pass.

Try:

<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 advice helped. I replaced the foreach loop with a for loop and used 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>
        <% } %>

I also changed the top line of the the view to (previously IEnumerable, now List).

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

The List<ConfirmVehicleModel> model parameter of the HttpPost method is now populated.

0

精彩评论

暂无评论...
验证码 换一张
取 消