ASP.NET MVC,Bootstrap Tables,获取每列的值

在ASP.NET MVC中,我有一个操作,它接受用户对行和列的输入,然后导航到根据用户输入生成所需行数和列数的操作,如下所示: –

浏览次数:

@using (Html.BeginForm()) { @for (int k = 0; k < Model.Rows.Capacity; k++) { @for (int i = 0; i < Model.Columns.Capacity; i++) { } }
@Html.TextBox("name" + k + i, null, new { @class = "form-control" })
}

进入后。

http://sofzh.miximages.com/c%23/lxhOM.png

在post动作中,我有一个表单集合,以便使用表单集合在post动作中获取填充的数据。 在post动作中,我使用foreach循环获取每个输入字段的值。

  [HttpPost] public ActionResult Enter(FormCollection form) { for (int i = 0; i < form.Count; i++) { Debug.WriteLine(form[i]); } return View(); } 

我只是想知道如何在post操作中获取单个列的所有值并逐列获取它们,默认情况下它获取水平值。

谢谢。 http://sofzh.miximages.com/c%23/SBMvY.png

创建表示要显示/编辑的视图模型

 public class TableVM { public TableVM() { Rows = new List(); } public TableVM(int rows, int columns) : this() { for(int i = 0; i < rows; i++) { Rows.Add(new RowVM(columns)); } } public List Headers { get; set; } // for columns headers public List Rows { get; set; } } public class RowVM { public RowVM() { Cells = new List(); } public RowVM(int columns) : this() { for(int i = 0; i < columns; i++) { Cells.Add(new CellVM()); } } public List Cells { get; set; } } public class CellVM { public string Value { get; set; } } 

并在GET方法中,初始化一个新的TableVM并将其返回到视图

 TableVM model = new TableVM(5, 5); // 5 x 5 grid model.Headers = new List{ "col 1", "col 2", "col 3", col 4", "col 5" }; return View(model); 

视图

 @model TableVM ....  @foreach(string header in Model.Headers) {  }  @for(int r = 0; r < Model.Rows.Count; r++) {  for (int c = 0; c < Model.Rows[r].Cells.Count; c++) {  }  } 
@header
@Html.TextBoxFor(m => m.Rows[r].Cells[c].Value

然后更改POST方法以接受模型

 [HttpPost] public ActionResult Enter(TableVM model) 

您现在可以使用索引器访问每个行/列,例如model.Rows[1].Cells[2].Value将返回第二行中第3列的值

最简单的方法是使用输入值返回行数和列数:

 
@using (Html.BeginForm()) { @for (int k = 0; k < Model.Rows.Capacity; k++) { @for (int i = 0; i < Model.Columns.Capacity; i++) { } }
@Html.TextBox("name" + k + i, null, new { @class = "form-control" })
}

获得这些后,您可以使用它们来计算要执行的迭代次数和要查询的输入名称:

 [HttpPost] public ActionResult Enter(FormCollection form) { var data = new List>(); var rows = int.Parse(form["rows"]); var columns = int.Parse(form["columns"]); for (var r = 0; r < rows; r++) { var rowData = new List(); for (var c = 0; c < columns; c++) { rowData.Add(form[string.Format("name{0}{1}", r, c)]); } data.Add(rowData); } return View(data); } 
 to get value horizontally you have to put name attribute in your view like  @{ foreach (var item in Model.t1_List) {  } } 
Name Allocation Percentage Allocation
@item.Name
and then at the time of post or in action method you can get this as Dictionary _d_amt = new Dictionary(); Dictionary _d_per = new Dictionary(); foreach (var key in _frm.AllKeys) { if (key.ParseString().Contains("t1_txtamt-")) { decimal _val = _frm[key].ParseString().ParseDecimal(); decimal _id = key.Replace("t1_txtamt-", "").ParseString().ParseDecimal(); _d_amt.Add(_id, _val); } else if (key.ParseString().Contains("t1_txtper-")) { decimal _val = _frm[key].ParseString().ParseDecimal(); decimal _id = key.Replace("t1_txtper-", "").ParseString().ParseDecimal(); _d_per.Add(_id, _val); } } foreach (var item in _model.t1_List) { AllocationClass _litem = new AllocationClass(); _litem.BId = item.BId; _litem.Name = item.Name; _litem.AllocationPercentage = (from p in _d_per where p.Key == item.BId select p.Value).FirstOrDefault().ParseDecimal(); _litem.AllocationAmount = (from p in _d_amt where p.Key == item.BId select p.Value).FirstOrDefault().ParseDecimal(); _list.Add(_litem); }