我有一个表格(在Web表单中)和操作结果.
<form action="ActionStockNew" method="post" id="form">
<table>
<tr>
<td><input type="text" name="f[0]StockId" /></td>
<td><input type="text" name="f[0]Amount" /></td>
<td><input type="text" name="f[0]Price" /></td>
</tr>
<tr>
<td><input type="text" name="f[1]StockId" /></td>
<td><input type="text" name="f[1]Amount" /></td>
<td><input type="text" name="f[1]Price" /></td>
</tr>
<tr>
<td><input type="text" name="f[2]StockId" /></td>
<td><input type="text" name="f[2]Amount" /></td>
<td><input type="text" name="f[2]Price" /></td>
</tr>
...
</table>
</form>
Run Code Online (Sandbox Code Playgroud)
行动结果;
[HttpPost]
public ActionResult ActionStockNew(FormCollection f)
{
foreach (var key in f.AllKeys.Where(q => q.StartsWith("f")).ToArray())
{
string abba = f[key];
}
return View();
}
Run Code Online (Sandbox Code Playgroud)
如何逐行读取发布的网格数据.
例如第一行数据;
f[i]StockId
f[i]Amount
f[i]Price
Run Code Online (Sandbox Code Playgroud)
谢谢.
Sar*_*nga 11
您可以创建一个Model Stock,它可以绑定到您的视图.然后您可以将库存对象列表传递给控制器,如下所示.
股票模型
public class Stock
{
public int StockId { get; set; }
public int Amount { get; set; }
public decimal Price { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
视图
@model IEnumerable<Stock>
<form action="/Controler/ActionStockNew" method="post" id="form">
<table>
@for (int i = 0; i < Model.Count(); i++)
{<tr>
<td>
<input type="text" name="[@i].StockId" />
</td>
<td>
<input type="text" name="[@i].Amount" />
</td>
<td>
<input type="text" name="[@i].Price" />
</td>
</tr>
}
</table>
<input type="submit" value="Save" />
</form>
Run Code Online (Sandbox Code Playgroud)
控制器
public ActionResult ActionStockNew()
{
List<Stock> stockList = new List<Stock>();
// fill stock
return View(stockList);
}
[HttpPost]
public ActionResult ActionStockNew(ICollection<Stock> stockList)
{
// process
}
Run Code Online (Sandbox Code Playgroud)
谢谢!