English 中文(简体)
MVC: 模型未提交多种形式
原标题:MVC : Model does not submit for multiple forms

这与伙伴关系有关。 Net MVC 2 P-4, 2 P-3, 2 FS, 2 NS

我有一个模型(Movies For All),符合我的意见,这一模式收集了ICollection。 我希望我的使用者在某个时候购买单张机票,点击提交纽顿·布费票,因此,我除了代表机票细节的每行外,还放下了一个顿子。 这导致了顺序的推移。

<%@Page Title="title" Language="C#" MasterPageFile="~/Views/Shared/MyMaster.Master" 
Inherits="System.Web.Mvc.ViewPage<OnlineBooking.Movies.MoviesForAll>">

<Table>
  <% for(i=0; i<Model.Tickets.count; i++)
  { %>
  <tr>
      <% using (Html.BeginForm(myaction(), FormMethod.Post, new {id="myform"})) 
         { %>
           <td>
             <%= Model.tickets[i].ticketID %>
             <%= Html.HiddenFor(m=>m.tickets[i].ticketID) %>
           </td>
           <td>
             <%= Model.tickets[i].seatNo %>
             <%= Html.HiddenFor(m=>m.tickets[i].seatNo) %>
           </td>
           ...
           ...
           <td>
             <input type="submit" value="buy this ticket" runat="server">
           </td>
      <% } %>
  </tr>
  <% } %>
</table>

这为每一轮单次提交纽顿的机票创造了一种形式。

问题在于,当我提交第一行(编制指数0)时,他点击了 but子,然后适当提交,我可以看到控制器方法中的数值。 但是,即使我利用隐蔽的田地对控制者进行约束,也没有任何其他行经。

我在这里找不到任何具体指数或东西?

提前感谢。

最佳回答

我建议,所有各行各有一种形式,然后每行各行各行各行各行各用地子元件,而不是投入如下:

<button type="submit" value="<%= Model.tickets[i].ticketID %>" name="buyticket">Buy this ticket</button>

你们应当对你们的控制者称为购买品的行动有一个参数,而且应该有点票,以点击的纽托。

问题回答

如何:

<table>
  <% for(i = 0; i < Model.Tickets.Count; i++) { %>
  <tr>
      <% using (Html.BeginForm("myaction", FormMethod.Post, new { id = "myform" })) { %>
        <td>
           <%= Model.tickets[i].ticketID %>
         </td>
         <td>
           <%= Model.tickets[i].seatNo %>
         </td>
         ...
         ...
         <td>
           <%= Html.Hidden("TicketId", Model.tickets[i].ticketID) %>
           <%= Html.Hidden("SeatNumber", Model.tickets[i].seatNo) %>
           <input type="submit" value="buy this ticket" />
         </td>
      <% } %>
  </tr>
  <% } %>
</table>

之后:

public class TicketToPurchaseViewModel
{
    public int TicketId { get; set; }
    public int SeatNumber { get; set; }
}

以及行动:

[HttpPost]
public ActionResult myaction(TicketToPurchaseViewModel ticket)
{
    ...
}

说明:我注意到,我已经从提交年份中删除了<代码>runat=“server”的属性,因为这肯定是你不想在一份合同中看到的。 NET MVC应用程序。





相关问题
Custom Binding in ASP.NET MVC with naming-conventions

I ve got a View where I use a naming-convention on my text-fields, to indicate what should be done with the content once it is posted back to my controller. The format is similar to: <input type="...

Tinymce Model Binding with ASP.NET MVC

Does anyone know how to auto-populate the tinymce editor from the ASP.NET MVC model? In other words, what code do I need to use to set the contents of tinymce? EDITED: My problem is that tinyMCE is ...

MVC Model Binding

I am using the MVC validation library from link text. I chose this library because I am also using .NetTiers which generates all of the Validation Attributes using MS Enterprise Library Validation ...

Exception Causes ModelBinding To Fail

If I receive a generic error in my ASP.NET MVC 2.0 Beta app and catch it. I notice that the next time I post that the model returned to my post is always null. Do I need to clear out the ModelState ...

热门标签