1

我有一个强类型的局部视图,它以列表格式显示输出。我想从数据库的部分视图中插入数据。我正在努力遍历部分视图中的行并将它们发送到控制器操作以插入数据库。这是我到目前为止所拥有的

部分的:

<%@ 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>

这是我在master中访问以上部分的方式:

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

控制器:

[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();

}

在部分视图中,列出了不止一名学生(2,3,4.. 行)。当我点击“保存”按钮时,我只能保存部分(第一行)中列出的第一个学生,如何循环访问部分列出的剩余学生以保存他们?

提前致谢。

4

3 回答 3

2

你的绑定错误,你可以从这个示例绑定开始,如果有任何错别字,对不起,我是在编辑器本身输入的。

更新:

您的部分:

<%@ 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>

您的主视图:

<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>

你的控制器:

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

        }
于 2012-07-17T20:21:56.930 回答
1

尝试为 MVC 绑定将识别为对象集合的输入名称命名:

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

然后您可以使用 anIEnumerable<Student.Models.vwStudent>作为您的模型类型,如 tsegay 所述。

于 2012-07-17T20:23:19.167 回答
0

您需要将模型绑定到集合。目前它只是绑定到单个值。

查看:http ://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

于 2012-07-17T20:25:04.073 回答