简体   繁体   English

使用context.request获取json数据时出现问题

[英]Issue while getting the json data using context.request

I'm developing a web based application using bootstrap. 我正在使用引导程序开发基于Web的应用程序。 I'm trying to implement inline editing in my grid on page load but im facing some issue while handling json data. 我正在尝试在页面加载时在网格中实现内联编辑,但是在处理json数据时遇到一些问题。

Here is my code : 这是我的代码:

  $(document).ready(function () {
    var GetUrl = Web_Path + '/Test/TestHandler/GetTestData/' + AjaxHandlerName;

                jQuery("#jqGrid-container").jqGrid({
                    url: GetUrl,
                    datatype: 'json',
                    mtype: 'POST',
                    postData: { SearchInfo: function () { return getSearchPostData() } },
                    colNames: [' ', 'ID', 'Name', 'ContactNo', 'EmpId', 'MailId', 'RoleName'],
                    colModel: [
                    { name: 'myac', index: '', width: 80, fixed: true, sortable: false, resize: false,
                        formatter: 'actions',
                        formatoptions: {
                            keys: true,
                            delOptions: { recreateForm: true, beforeShowForm: beforeDeleteCallback }
                        }
                    },
                                { name: 'Id', index: 'Id', hidden: true, editable: true },
                                { name: 'Name', index: 'Name', validation: { required: true }, sortable: true, editable: true, editoptions: { size: "40", maxlength: "50"} },
                                { name: 'ContactNo', index: 'ContactNo', sortable: false, editable: true, editoptions: { size: "20", maxlength: "30"} },
                                { name: 'EmpId', index: 'EmpId', sortable: false, editable: true, editoptions: { size: "20", maxlength: "30"} },
                                { name: 'MailId', index: 'MailId', sortable: false, editable: true, editoptions: { size: "40", maxlength: "50"} },
 {name: 'RoleName', index: 'RoleName', sortable: false }
  ],


                    editurl: ISM_Web_Path + '/Test/TestHandler/UpdateTestContacts/' + ISMAjaxHandlerName,                 

                    ajaxRowOptions: {
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json"
                    },                 
                    serializeRowData: function (postdata) {
                    return { ContactInfo: JSON.stringify(postdata) };                     
                    },
                    jsonReader: {
                        id: 'Id',
                        repeatitems: false
                    },
                    height: "100%",
                    pager: '#jqGrid-pager',
                    rowNum: 10,
                    rowList: [10, 20, 30],
                    sortname: 'Id',
                    sortorder: 'desc',
                    viewrecords: true,
                    caption: "JQ grid data",
                    loadComplete: function () {
                        var table = this;
                        updatePagerIcons(table);                       
                    }
                });
});

 function getSearchPostData() {
            var searchData = {};
            searchData.Id=1;

            return JSON.stringify(searchData);
        }
 function updatePagerIcons(table) {
            var replacement =
                    {
                        'ui-icon-seek-first': 'icon-double-angle-left bigger-140',
                        'ui-icon-seek-prev': 'icon-angle-left bigger-140',
                        'ui-icon-seek-next': 'icon-angle-right bigger-140',
                        'ui-icon-seek-end': 'icon-double-angle-right bigger-140'
                    };
            $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function () {
                var icon = $(this);
                var $class = $.trim(icon.attr('class').replace('ui-icon', ''));

                if ($class in replacement) icon.attr('class', 'ui-icon ' + replacement[$class]);
            })
}

 <div class="row">
        <div class="col-xs-12">           
            <table id="jqGrid-container" class="ui-jqgrid ui-widget ui-widget-content ui-corner-all">
            </table>
            <div id="jqGrid-pager">
            </div>           
        </div>
    </div>

Handler function 处理函数

 public void UpdateTestContacts(HttpContext context)
        {
            TestContact contactInfo =new TestContact();
            string jsonData = context.Request.Params["ContactInfo"];
            MemoryStream TestContactMs = new MemoryStream(Encoding.UTF8.GetBytes(jsonData));
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(TestContact));
            contactInfo = (RelationshipContact)serializer.ReadObject(TestContactMs );              
            //call manger function
        }

TestContact.cs TestContact.cs

 public class TestContact
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string ContactNo { get; set; }

        public string EmpId { get; set; }

        public int RelId { get; set; }

        public int TypeId { get; set; }

        public string MailId { get; set; }

        public string RoleName { get; set; }       
    }

I have used jquery.jqGrid.min.js . 我用过jquery.jqGrid.min.js

I'm getting and error ArgumentNullException was handled by User Code.String reference not set to an instance of a String. 我收到错误ArgumentNullException由用户Code.String引用处理,未设置为String的实例。 Parameter name: s in string jsonData = context.Request.Params["ContactInfo"]; 参数名称: 字符串jsonData = context.Request.Params [“ ContactInfo”];中的 s

Please help me out. 请帮帮我。

Already it is in JSon type, so i have removed the below lines from my code. 它已经是JSon类型,因此我从代码中删除了以下几行。

 ajaxRowOptions: {
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json"
                    }

It works fine.. 它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM