Reputation: 43
I displayed a list of data in form of a grid [basic html table] and placed text boxes so that we can edit inside the grid and post the values so that I can save it. The list is not big , around 5-10 rows in it.
How to access these form values back in controller? FormCollection
doesn't seem to work and I cannot even access the values through Request.Form[]
. I want it back in a form of a list so that I can loop through it and get the new values.
.cshtml
<form action="/Parameter/StudentWeights" method="post">
<table>
<tr>
<th>
Category
</th>
<th>
CategoryAlias
</th>
<th>
YearCode
</th>
<th>
ClassKto8
</th>
<th>
Class9to12
</th>
<th></th>
</tr>
@foreach(var item in Model.StudentWeights) {
<tr>
<td>
@Html.HiddenFor(modelItem => item.CategoryId)
</td>
<td>
@Html.DisplayFor(modelItem => item.Category)
</td>
<td>
@Html.DisplayFor(modelItem => item.CategoryAlias)
</td>
<td>
@Html.DisplayFor(modelItem => item.YearCode)
</td>
<td>
@Html.EditorFor(modelItem => item.ClassKto8)
</td>
<td>
@Html.EditorFor(modelItem => item.Class9to12)
</td>
</tr>
}
</table>
<input type="submit" value = "Submit" />
</form>
controller
[HttpPost]
public ActionResult studentWeights(FormCollection collection)
{
try
{
// TODO: Add update logic here
//service.
foreach (var item in collection)
{
int x = item. // i want to loop through it and access the values.
}
}
catch
{
return View();
}
}
please help me how to get these values. I don't want to use JEditable or any third party jQuery tools.
Is there any way to create a custom type and assign values in JavaScript or jQuery upon button click and then send it to my controller action?
Thank you very much, any suggestion would be much helpful.
Upvotes: 3
Views: 2321
Reputation: 3508
You need to access the form collection differently. The form collection is a key value pair of the values posted to the controller. formcollection["postvalue"]
[HttpPost]
public ActionResult studentWeights(FormCollection formCollection)
{
foreach (string _formData in formCollection)
{
var x = formCollection[_formData];
}
}
Here are several way to iterate through a form collection http://stack247.wordpress.com/2011/03/20/iterate-through-system-web-mvc-formcollection/
Upvotes: 1