问题描述:

I have a strongly-typed partial view which displays output in a list format. I want to insert data from partial view in DB. I am struggling with looping through rows in partial view and sending them to controller action to be inserted in 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:

<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). 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?

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:

       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.

网友答案:

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

相关阅读:
Top