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 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.