[英]Pass data from partial view to controller action
我有一個強類型的局部視圖,以列表格式顯示輸出。 我想在DB中插入部分視圖中的數據。 我正在努力在局部視圖中循環遍歷行並將它們發送到控制器操作以插入到DB中。 這是我到目前為止所擁有的
部分:
<%@ 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 ..行)。 當我點擊“保存”按鈕時,我只能保存在Partial(第1行)中列出的第一個學生,如何循環顯示部分中列出的剩余學生以保存它們?
提前致謝。
你的綁定錯了,你可以從這個示例綁定開始,對不起,如果有任何拼寫錯誤,我在編輯器本身輸入。
更新:
你的偏愛:
<%@ 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);
}
嘗試提供MVC綁定將識別為對象集合的輸入名稱:
int index = 0;
foreach (var item in Model) { %>
<td>
<%=Html.TextBox(string.Format("Entry[{0}].StudentID", index),
item.StudentID) %>
</td>
...
<%
i++;
} %>
然后你可以使用IEnumerable<Student.Models.vwStudent>
作為你的模型類型,正如tsegay所描述的那樣。
您需要對集合執行模型綁定。 目前它只是綁定到單個值。
退房: http : //haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.