各位:
我有個table叫"customer
public partial class customer
{
public string cust_id { get; set; }
public string cust_name { get; set; }
}
在controller讀取資料後傳到前端的view
controller如下:
public ActionResult form03()
{
return View(db.customer.ToList());
}
view如下:
@model IEnumerable<customer>
@using (Html.BeginForm())
{
<input type="submit" value="Submit" /><br />
<table>
<tr>
<td>Customer ID</td>
<td>Customer Name</td>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.EditorFor(model => item.cust_id)
</td>
<td>
@Html.EditorFor(model => item.cust_name)
</td>
</tr>
}
</table>
}
請問各位大大, 我在按Submit後, 如何讀取輸入的資料
如果你真的要抓多筆資料
你就無法用MVC預設的Model Binding
你可以試試使用
HttpContext.Request.Form;
HttpContext.Request.QueryString
來處理表單提交的資料
其實早上終於找到解決的方法了, 就是將foreach改為for, IEnumerable改為List就可以了
修正後的View如下
@model List<customer> ==>修改
@using (Html.BeginForm())
{
<input type="submit" value="Submit" /><br />
<table>
<tr>
<td>Customer ID</td>
<td>Customer Name</td>
</tr>
@for (int i = 0; i < Model.Count; i++) ==>修改
{
<tr>
<td>@Html.EditorFor(m => m[i].cust_id)</td> =>改
<td>@Html.EditorFor(m => m[i].cust_name)</td>
</tr>
}
</table>
}
接收的controller
[HttpPost]
public ActionResult form03(List<customer> model)
{
customer customers;
List<customer> list = new List<customer>();
for (int i=0;i<model.ToList().Count;i++)
{
customers = model[i];
list.Add(customers);
}
return View(list);
}