0

我浏览了很多关于这个主题的帖子,但到目前为止我还没有解决我的问题。我是 MVC / Jquery 的新手,所以我可能错过了答案。

我希望从 jqGrid 返回所有行。我使用 Oleg 的示例代码进行 jqGrid 处理:

    sendData = function (data) {
        var dataToSend = JSON.stringify(data);
        alert("The following data are sending to the server:\n" + dataToSend);
        $.ajax({
            type: "POST",
            url: '@Url.Action("Update", "WatchList")',
            dataType: "json",
            data: dataToSend,
            loadonce: true,
            contentType: "application/json; charset=utf-8",
            success: function (response, textStatus, jqXHR) {
                // remove error div if exist
                $('#' + grid[0].id + '_err').remove();
                alert("Updates Complete");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                // remove error div if exist
                $('#' + grid[0].id + '_err').remove();
                // insert div with the error description before the grid
                grid.closest('div.ui-jqgrid').before(
                    '<div id="' + grid[0].id + '_err" style="max-width:' + grid[0].style.width +
                    ';"><div class="ui-state-error ui-corner-all" style="padding:0.7em;float:left;"><span class="ui-icon ui-icon-alert" ' +
                    'style="float:left; margin-right: .3em;"></span><span style="clear:left">' +
                    decodeErrorMessage(jqXHR, textStatus, errorThrown) + '</span></div><div style="clear:left"/></div>');
            }

        $("#sendAll").click(function () {
            var localGridData = grid.jqGrid('getGridParam', 'data');
            sendData(localGridData);
        });

    };

帖子的提琴手输出是:

POST http://localhost:49323/WatchList/Update HTTP/1.1
Host: localhost:49323
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:49323/WatchList/Edit
Content-Length: 3645
Cookie: __RequestVerificationToken=flUoW7U12gHRfOVMNiLu_tW4G_oVI4TwxvAYKOU9arGI014KWFKUKjUbgjbZStLb7n1TGmU22NtoQjAb26bmjcccjg8x6skcL1zbP_o2y5I1; .ASPXAUTH=D968251D718DEF7CA0C9D4A6BD2750F9923CBF554FC0B058772C753C4305682578C207F7367B6D61F80F84CBDCA5C130126AE2CC5E1715FA897122DEB68B3B40C86816C32A6F047891794A732F4A7A3499064220077793B5F1313813FFC21D01
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

[{"Date":"Fri 22 March 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"0"},{"Date":"Sat 23 March 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"1"},{"Date":"Sun 24 March 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"2"},{"Date":"Mon 25 March 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"3"},{"Date":"Tue 26 March 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"4"},{"Date":"Wed 27 March 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"5"},{"Date":"Thu 28 March 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"6"},{"Date":"Fri 29 March 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"7"},{"Date":"Sat 30 March 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"8"},{"Date":"Sun 31 March 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"9"},{"Date":"Mon 1 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"10"},{"Date":"Tue 2 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"11"},{"Date":"Wed 3 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"12"},{"Date":"Thu 4 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"13"},{"Date":"Fri 5 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"14"},{"Date":"Sat 6 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"15"},{"Date":"Sun 7 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"16"},{"Date":"Mon 8 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"17"},{"Date":"Tue 9 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"18"},{"Date":"Wed 10 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"19"},{"Date":"Thu 11 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"20"},{"Date":"Fri 12 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"21"},{"Date":"Sat 13 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"22"},{"Date":"Sun 14 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"23"},{"Date":"Mon 15 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"24"},{"Date":"Tue 16 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"25"},{"Date":"Wed 17 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"26"},{"Date":"Thu 18 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"27"},{"Date":"Fri 19 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"28"},{"Date":"Sat 20 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"29"},{"Date":"Sun 21 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"30"},{"Date":"Mon 22 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"31"},{"Date":"Tue 23 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"32"}]

控制器接收到的参数为空?我没有将数据直接导入的模型,因为网格列是动态创建的,因此我需要解析原始数据。在我的控制器操作中我需要什么来获取发布数据?

4

2 回答 2

0

您的更改必须关闭,因为现在正在识别数据但出现错误

 Invalid JSON primitive: griddata.]
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: Invalid JSON primitive: griddata.

[ArgumentException: Invalid JSON primitive: griddata.]
   System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject() +915
   System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +597
   System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) +108
   System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +211
   System.Web.Script.Serialization.JavaScriptSerializer.DeserializeObject(String input) +86
   System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext) +298
   System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +123
   System.Web.Mvc.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory) +59
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +145
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +171
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +460
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
   System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) +360
   System.Web.Mvc.ControllerBase.get_ValueProvider() +85
   System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +154
   System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +199
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +978
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag) +53
   System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +523
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +83
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
   System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +501
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
   System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +417
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +49
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +127
   System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +161
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate`1 endDelegate, Object tag, Int32 timeout) +92
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag, Int32 timeout) +97
   System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) +53
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +373
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

我注意到,如果我使用原始代码:

    data: dataToSend,

帖子数据在 Fiddler 中显示为:

[{"Date":"Fri 29 March 2013","id":"0"},{"Date":"Sat 30 March 2013","id":"1"},{"Date":"Sun 31 March 2013","id":"2"},{"Date":"Mon 1 April 2013","id":"3"},{"Date":"Tue 2 April 2013","id":"4"},{"Date":"Wed 3 April 2013","id":"5"},{"Date":"Thu 4 April 2013","id":"6"},{"Date":"Fri 5 April 2013","id":"7"},{"Date":"Sat 6 April 2013","id":"8"},{"Date":"Sun 7 April 2013","id":"9"},{"Date":"Mon 8 April 2013","id":"10"},{"Date":"Tue 9 April 2013","id":"11"},{"Date":"Wed 10 April 2013","id":"12"},{"Date":"Thu 11 April 2013","id":"13"},{"Date":"Fri 12 April 2013","id":"14"},{"Date":"Sat 13 April 2013","id":"15"},{"Date":"Sun 14 April 2013","id":"16"},{"Date":"Mon 15 April 2013","id":"17"},{"Date":"Tue 16 April 2013","id":"18"},{"Date":"Wed 17 April 2013","id":"19"},{"Date":"Thu 18 April 2013","id":"20"},{"Date":"Fri 19 April 2013","id":"21"},{"Date":"Sat 20 April 2013","id":"22"},{"Date":"Sun 21 April 2013","id":"23"},{"Date":"Mon 22 April 2013","id":"24"},{"Date":"Tue 23 April 2013","id":"25"},{"Date":"Wed 24 April 2013","id":"26"},{"Date":"Thu 25 April 2013","id":"27"},{"Date":"Fri 26 April 2013","id":"28"},{"Date":"Sat 27 April 2013","id":"29"},{"Date":"Sun 28 April 2013","id":"30"},{"Date":"Mon 29 April 2013","id":"31"},{"Date":"Tue 30 April 2013","Night":"False","Morning":"False","Afternoon":"False","Evening":"False","id":"32"}]

如果使用

数据:{griddata:dataToSend},

数据在 Fiddler 中显示为:

griddata=%5B%7B%22Date%22%3A%22Fri+29+March+2013%22%2C%22id%22%3A%220%22%7D%2C%7B%22Date%22%3A%22Sat+30+March+2013%22%2C%22id%22%3A%221%22%7D%2C%7B%22Date%22%3A%22Sun+31+March+2013%22%2C%22id%22%3A%222%22%7D%2C%7B%22Date%22%3A%22Mon+1+April+2013%22%2C%22id%22%3A%223%22%7D%2C%7B%22Date%22%3A%22Tue+2+April+2013%22%2C%22id%22%3A%224%22%7D%2C%7B%22Date%22%3A%22Wed+3+April+2013%22%2C%22id%22%3A%225%22%7D%2C%7B%22Date%22%3A%22Thu+4+April+2013%22%2C%22id%22%3A%226%22%7D%2C%7B%22Date%22%3A%22Fri+5+April+2013%22%2C%22id%22%3A%227%22%7D%2C%7B%22Date%22%3A%22Sat+6+April+2013%22%2C%22id%22%3A%228%22%7D%2C%7B%22Date%22%3A%22Sun+7+April+2013%22%2C%22id%22%3A%229%22%7D%2C%7B%22Date%22%3A%22Mon+8+April+2013%22%2C%22id%22%3A%2210%22%7D%2C%7B%22Date%22%3A%22Tue+9+April+2013%22%2C%22id%22%3A%2211%22%7D%2C%7B%22Date%22%3A%22Wed+10+April+2013%22%2C%22id%22%3A%2212%22%7D%2C%7B%22Date%22%3A%22Thu+11+April+2013%22%2C%22id%22%3A%2213%22%7D%2C%7B%22Date%22%3A%22Fri+12+April+2013%22%2C%22id%22%3A%2214%22%7D%2C%7B%22Date%22%3A%22Sat+13+April+2013%22%2C%22id%22%3A%2215%22%7D%2C%7B%22Date%22%3A%22Sun+14+April+2013%22%2C%22id%22%3A%2216%22%7D%2C%7B%22Date%22%3A%22Mon+15+April+2013%22%2C%22id%22%3A%2217%22%7D%2C%7B%22Date%22%3A%22Tue+16+April+2013%22%2C%22id%22%3A%2218%22%7D%2C%7B%22Date%22%3A%22Wed+17+April+2013%22%2C%22id%22%3A%2219%22%7D%2C%7B%22Date%22%3A%22Thu+18+April+2013%22%2C%22id%22%3A%2220%22%7D%2C%7B%22Date%22%3A%22Fri+19+April+2013%22%2C%22id%22%3A%2221%22%7D%2C%7B%22Date%22%3A%22Sat+20+April+2013%22%2C%22id%22%3A%2222%22%7D%2C%7B%22Date%22%3A%22Sun+21+April+2013%22%2C%22id%22%3A%2223%22%7D%2C%7B%22Date%22%3A%22Mon+22+April+2013%22%2C%22id%22%3A%2224%22%7D%2C%7B%22Date%22%3A%22Tue+23+April+2013%22%2C%22id%22%3A%2225%22%7D%2C%7B%22Date%22%3A%22Wed+24+April+2013%22%2C%22id%22%3A%2226%22%7D%2C%7B%22Date%22%3A%22Thu+25+April+2013%22%2C%22id%22%3A%2227%22%7D%2C%7B%22Date%22%3A%22Fri+26+April+2013%22%2C%22id%22%3A%2228%22%7D%2C%7B%22Date%22%3A%22Sat+27+April+2013%22%2C%22id%22%3A%2229%22%7D%2C%7B%22Date%22%3A%22Sun+28+April+2013%22%2C%22id%22%3A%2230%22%7D%2C%7B%22Date%22%3A%22Mon+29+April+2013%22%2C%22id%22%3A%2231%22%7D%2C%7B%22Date%22%3A%22Tue+30+April+2013%22%2C%22Night%22%3A%22False%22%2C%22Morning%22%3A%22False%22%2C%22Afternoon%22%3A%22False%22%2C%22Evening%22%3A%22False%22%2C%22id%22%3A%2232%22%7D%5D
于 2013-03-30T16:34:31.813 回答
0

尝试像这样修改:

 function sendData(data) {
        var dataToSend = JSON.stringify(data);
        alert("The following data are sending to the server:\n" + dataToSend);
        $.ajax({
            type: "POST",
            url: '@Url.Action("WatchList", "Update")',
            dataType: "json",
            data: {griddata:dataToSend},
            loadonce: true,
            contentType: "application/json; charset=utf-8",
            success: function (response, textStatus, jqXHR) {
                // remove error div if exist
                $('#' + grid[0].id + '_err').remove();
                alert("Updates Complete");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                // remove error div if exist
                $('#' + grid[0].id + '_err').remove();
                // insert div with the error description before the grid
                grid.closest('div.ui-jqgrid').before(
                    '<div id="' + grid[0].id + '_err" style="max-width:' + grid[0].style.width +
                    ';"><div class="ui-state-error ui-corner-all" style="padding:0.7em;float:left;"><span class="ui-icon ui-icon-alert" ' +
                    'style="float:left; margin-right: .3em;"></span><span style="clear:left">' +
                    decodeErrorMessage(jqXHR, textStatus, errorThrown) + '</span></div><div style="clear:left"/></div>');
            }

        $("#sendAll").click(function () {
            var localGridData = grid.jqGrid('getGridParam', 'data');
            sendData(localGridData);
        });

    };

在控制器中

public ActionResult WatchList(string griddata) { 
........
}
于 2013-03-30T03:27:41.060 回答