Note that there are some explanatory texts on larger screens.

plurals
  1. POPass data from partial view to controller action
    primarykey
    data
    text
    <p>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</p> <p>Partial:</p> <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;IEnumerable&lt;Student.Models.vwStudent&gt;&gt;" %&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt; Student ID &lt;/th&gt; &lt;th&gt; Past Due Amount &lt;/th&gt; &lt;th&gt; Past Due Days &lt;/th&gt; &lt;/tr&gt; &lt;% if (Model != null) foreach (var item in Model) { %&gt; &lt;tr&gt; &lt;td&gt; &lt;%=Html.TextBox("StudentID",item.StudentID) %&gt; &lt;/td&gt; &lt;td&gt; &lt;%=Html.TextBox("PastDueAmount",item.PastDueAmount) %&gt; &lt;/td&gt; &lt;td&gt; &lt;%=Html.TextBox("PastDueDays",item.PastDueDays) %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;% } %&gt; &lt;/table&gt; </code></pre> <p>Here is how I access above partial in master:</p> <pre><code>&lt;div&gt; &lt;% using(Html.BeginForm("SaveStudentInvoice", "StudentInvoice")) %&gt; &lt;% { %&gt; &lt;div&gt; &lt;% Html.RenderPartial("DisplayStudentInvoiceList"); %&gt; &lt;/div&gt; &lt;% } %&gt; &lt;br/&gt; &lt;input type="button" id="Submit" name="Submit" value="Submit" /&gt; &lt;/div&gt; </code></pre> <p>Controller:</p> <pre><code>[HttpPost()] public ActionResult SaveStudentInvoice(IEnumerable&lt;Student.Models.vwStudent&gt; 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(); } </code></pre> <p>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?</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload