简体   繁体   English

将数据从局部视图传递到控制器操作

[英]Pass data from partial view to controller action

I have a strongly-typed partial view which displays output in a list format. 我有一个强类型的局部视图,以列表格式显示输出。 I want to insert data from partial view in DB. 我想在DB中插入部分视图中的数据。 I am struggling with looping through rows in partial view and sending them to controller action to be inserted in DB. 我正在努力在局部视图中循环遍历行并将它们发送到控制器操作以插入到DB中。 Here is what i have so far 这是我到目前为止所拥有的

Partial: 部分:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Student.Models.vwStudent>>" %>

<table>
        <tr>
            <th>
                Student ID
            </th>           
            <th>
                Past Due Amount
            </th>
            <th>
                Past Due Days
            </th>            
        </tr>

        <% if (Model != null)
           foreach (var item in Model) { %>

        <tr>
            <td>
                <%=Html.TextBox("StudentID",item.StudentID) %>
            </td>

            <td>
                <%=Html.TextBox("PastDueAmount",item.PastDueAmount) %>
            </td>
            <td>
                <%=Html.TextBox("PastDueDays",item.PastDueDays) %>
            </td>            
        </tr>

    <% } %>

    </table>

Here is how I access above partial in master: 以下是我在master中访问以上部分的方法:

<div>
<% using(Html.BeginForm("SaveStudentInvoice", "StudentInvoice")) %>
<% { %>
<div>
<% Html.RenderPartial("DisplayStudentInvoiceList"); %>
</div>
<% } %>
<br/>
<input type="button" id="Submit" name="Submit" value="Submit" />
</div>

Controller: 控制器:

[HttpPost()]
public ActionResult SaveStudentInvoice(IEnumerable<Student.Models.vwStudent> students)
{
    DataContext db = new DataContext();

foreach(Student.Models.vwStudent student in students){

        Invoice Inv = new Invoice();
        {
            Inv.StudentID= student.StudentID;
            Inv.PastDueAmount= student.PastDueAmount;   
            Inv.PastDueDays= student.PastDueDays;                 
        };

        db.Invoices.InsertOnSubmit(Inv);
}
        db.SubmitChanges();

        return View();

}

In the partial view, there are more than one students listed(2,3,4.. rows). 在局部视图中,列出了多个学生(2,3,4 ..行)。 When I click "Save" button I am only able to save the first student listed in Partial (1st row), How can I loop through the remaining students listed in partial to save them? 当我点击“保存”按钮时,我只能保存在Partial(第1行)中列出的第一个学生,如何循环显示部分中列出的剩余学生以保存它们?

Thanks in advance. 提前致谢。

You have your bindings wrong, You can start with this sample binding, sorry if there is any typo, I am typing in the editor itself. 你的绑定错了,你可以从这个示例绑定开始,对不起,如果有任何拼写错误,我在编辑器本身输入。

Updated: 更新:

Your Partial: 你的偏爱:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<SaveStudentInvoice.Models.vwStudent>>" %>

<table>
        <tr>
            <th>
                Student ID
            </th>           
            <th>
                Past Due Amount
            </th>
            <th>
                Past Due Days
            </th>            
        </tr>

        <% if (Model != null)
  for (int i = 0; i < Model.Count(); i++) { %>
        <tr>
            <td>
                <%=Html.TextBoxFor(m=>m.ToList()[i].StudentID) %>
            </td>

            <td>
                <%=Html.TextBoxFor(m=>m.ToList().ToList()[i].PastDueAmount) %>
            </td>
            <td>
                <%=Html.TextBoxFor(m=>m.ToList()[i].PastDueDays) %>
            </td>            
        </tr>

    <% } %>

    </table>

You Main View: 你主要观点:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
   <div>
<% using(Html.BeginForm("Index","Home")) %>
<% { %>
<div>
<% Html.RenderPartial("Partial",Model); %>
</div>
<br/>
<input type="submit" id="Submit" name="Submit" value="Submit" />
<% } %>

</div>
</asp:Content>

You Controller: 你控制器:

public ActionResult Index()
        {
            List<vwStudent> students = new List<vwStudent>();
            students.Add(new vwStudent() { PastDueAmount = 100, PastDueDays = "None", StudentID = "ooooo" });
            students.Add(new vwStudent() { PastDueAmount = 102, PastDueDays = "No", StudentID = "Sollina" });
            return View(students);

        }
        [HttpPost]
        public ActionResult Index(List<vwStudent> studentModel)
        {

            return View(studentModel);

        }

Try giving your inputs names that the MVC binding will recognize as being a collection of objects: 尝试提供MVC绑定将识别为对象集合的输入名称:

       int index = 0;
       foreach (var item in Model) { %>
        <td>
            <%=Html.TextBox(string.Format("Entry[{0}].StudentID", index),
                            item.StudentID) %>
        </td>
        ...
    <% 
           i++;
       } %>

Then you can use an IEnumerable<Student.Models.vwStudent> as your model type, as tsegay describes. 然后你可以使用IEnumerable<Student.Models.vwStudent>作为你的模型类型,正如tsegay所描述的那样。

You need to perform your model binding to a collection. 您需要对集合执行模型绑定。 At the moment it is just binding to single values. 目前它只是绑定到单个值。

Check out: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx 退房: http//haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM