1

我的情况是,我得到了一个要输出到 MVC 视图的数据集。我真的很难弄清楚如何让模型绑定器在提交后返回的路上捡起它。我有这样的东西...

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);
}

在页面上,我为输入元素尝试了许多不同的命名方案,但无济于事。这甚至可能吗。如果可能的话,我会使用视图模型类型,但不幸的是,这是一个完全动态的结构,而数据集是最自然的使用方式。

4

1 回答 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)

于 2009-12-08T12:19:24.470 回答