开发者

ASP.NET MVC 1: DataSet ModelBinding

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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜