views:

77

answers:

1

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 "grid 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

A: 

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
}
Sir Gallahad
I'm not so worried about how to create the code on the view as I am how do I access the table in the controller. I'm probably going to use a jQuery Grid like SlickGrid for the View but I'm curious how I can send that data to the Controller (possibly Ajax-ified send and not a full post back).So the 2 things I really need help with is this:1. How do I send the grid data from the view (which you touch on a bit).2. How do I access the sent data and what does my controller method signature look like and what does my controller accessing the grid code look like?
Paul
I've updated the post with some controller side code.
Sir Gallahad
This is very helpful and since you have responded so quickly I hate to keep adding more "requirements"/"questions" to my original question but any idea how to do this using a jQuery grid? I view source on the jQuery grid and I don't see the table being generated so I assume I have to fill out some JSON object on the jQuery side and post that to the controller method. Any thoughts on doing this with a standard jQuery grid (Like SlickGrid) instead of a home-grown one?
Paul
I'm looking in the SlickGrid code to give you an answer... But It may not be so fast. Give me a couple of hours... Meanwhile don't forget to vote up or down this answer.
Sir Gallahad
I would love to vote up your answer but I don't have the min required "rep" to vote up. However, once I get it I will be happy to do so...now off to go look for questions I can answer to increase my rep. =)
Paul