将 WebGrid 传递给控制器

Pass a WebGrid to Controller

我想将整个 WebGrid 发送到我的控制器并将其转换为 DataTable。

var grid1 = new WebGrid(Model.dsvm as IEnumerable<ASP_Upload_Version_1.Models.Share_Template>, canPage: true, canSort: false);
@grid1.GetHtml(tableStyle: "table table-sm table-striped table-condensed",
htmlAttributes: new { @id = "GridPractice", @class = "table table-sm table-striped-left table-bordered table-condensed", @style = "width:100%" },
columns: grid1.Columns(
    grid1.Column("PracticeArea", "Practice Area"),
    grid1.Column("MarketArea", "Market Area"),
    grid1.Column(format: @<text>
    <a data-title="Are you sure to deactivate this Input?" class="Delete"><i class="fa fa-trash" style="color:red"></i></a></text>, header: "Remove")));

因此,我可以将数据表作为用户定义的类型参数发送到 SQL 过程。

将 WebGrid 转换为 JSON 结果是一种好方法吗? 有没有更简单的选择?

我使用

将网格转换为 json
var _griddata = gridTojson("GridPractice");

并将其传递给控制器​​。在那里,我使用

将其转换为字典
Dictionary<string, string[]> log = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string[]>>(GridData);

然后我用下面的代码将数据移动到DataTable中

DataTable PracticeTable= new DataTable();

PracticeTable.Columns.Add("TestColumn1");
PracticeTable.Columns.Add("");

for (int i = 0; i < log.Count; i++)
{
    PracticeTable.Rows.Add(log["" + i + ""][0], log["" + i + ""][1]);
}