views:

383

answers:

1

I'm in a situation where I'm being given a dataset to output to an MVC view. I'm really struggling to figure out how to get the modelbinder to pick it up on the way back in after a submit. I have something like this...

public ActionResult TestData()
{
    DataSet data = new DataSet("DS");

    DataTable table = new DataTable("DT");
    data.Tables.Add(table);

    table.Columns.Add("ID", typeof (int));
    table.Columns.Add("Name", typeof (string));

    table.Rows.Add(1, "John");
    table.Rows.Add(2, "Mark");
    table.Rows.Add(3, "Paul");
    table.Rows.Add(4, "Chris");

    return View(data);
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult TestData(DataSet data)
{
    return View(data);
}

On the page I have tried lots of different naming schemes for the input elements but to no avail. Is this even possible. I would be using a viewmodel type if it were possible but unfortunately this is a completely dynamic structure and a dataset is the most natural thing to use.

+1  A: 

If it were me, I would serialize the rows into hidden fields that all have the same name attribute. Maybe something like

<input type="hidden" name="dsRows" value="[id]_[name]" />

Then on the second action (the one that accepts POST), accept a string[] as the parameter. You could then use a little LINQ query or something to "explode" the array into a dataset.

(note: I have not tested this specifically, but I've used the strategy several times)

Jay Querido
Thanks for the suggestion. This (or options like it) seem to be the way to go. I think I'll try to get my dataset into something more friendly though rather than use it vanilla.
Chris Meek