0%

ASP.NET MVC Table Grid Post Form And Read Lines

How can read the posted grid data line by line?

You can create a Model for Stock and it can be bind to your view. Then you can pass list of stock objects to controller as below.

Stock Model

1
2
3
4
5
6
public class Stock
{
public int StockId { get; set; }
public int Amount { get; set; }
public decimal Price { get; set; }
}

View

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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>

Controllers

1
2
3
4
5
6
7
8
9
10
11
12
13
public ActionResult ActionStockNew()
{
List<Stock> stockList = new List<Stock>();
// fill stock

return View(stockList);
}

[HttpPost]
public ActionResult ActionStockNew(ICollection<Stock> stockList)
{
// process
}