Best practice for sending Grid data in ASP.NET MVC 2 from View to Controller
I am new to ASP.NET MVC 2 and would like to know what the easiest and/or best practice is to send "grid data" from a View to a Controller. Think of this "grid data" as an excel spreadsheet where some columns will have some data and some rows will have some data. In the controller I need to know not only what column and row contains data but also the specific data it contains. My hope would be to have a "gri开发者_运维技巧d object" in the controller class that I could loop through to gather the necessary data but I'm open to other options.
Obviously this probably isn't the easiest way to capture data like this but just imagine this is the only way you are allowed to get this input from the user.
Also I am using VS.NET 2010, ASP.NET 4.0, C# 4.0 so I've thought about not using the ASP.NET MVC 2 and using ASP.NET web forms since it seems this solution would be easier in web forms. My only concern with doing that is there are other aspects of the website that would be easier in MVC and this seems to be the only issue at this point with using MVC.
Thanks, Paul
That will depend a great deal on how you've bilt your table. But here is a very simple sample:
1 - put your hole table in a form;
2 - Identify each data cell's field with it's "coorditate"
Like this:
<%: using(form = html.Form("UpdateGrid")) { %>
<table>
<% for (int r = 0; r < rowCount; r++) { %>
<tr>
<% for (int c = 0; c < columnCount; c++) { %>
<td><%: html.TextBox(string.format("cell_{0}_{1}",r,c)) %></td>
<% } %>
</tr>
<% } %>
</table>
<% } %>
It's just a sample code (I'm not sure if it will work as it is), but it will give you an idea. If you want a Excel like address you can use this:
<td><%: html.TextBox(string.format("{1}{0}",(char)(r + 'A'),c + 1)) %></td>
To access the table info from the controller you can use the following cone:
public ActionResult UpdateGrid(FormCollection form) {
// ... Some initialization
for (int r = 0; r < rowCount; r++ ) {
for (int c = 0; c < columnCount; c++ ) {
var cellValue = form[string.format("{1}{0}",(char)(r + 'A'),c + 1)]; // Excell like format
// Add your manipulation here;
}
}
// ... Continue your controller implementation
}
精彩评论