0

I have the Foreign key in JqGrid and show name of foreign key in grid.

when i want to edit, method don't send value of foreign key and value of foreign key is null:

public ActionResult EditState(tblState state)
    {
        UserRepository user = new UserRepository();
        state.UserIDChange_FK = Convert.ToInt64(user.FindUserID(User.Identity.Name));
        state.StateDateChange = (DateTime.Now);
        state.StateDateCreate = DateTimeConvertor.ToDateTime(state.StateDateCreate.ToString());
        if (ModelState.IsValid)
        {
            StateRepository mr = new StateRepository();
            if (mr.Update(state, true))
            {
                return MessageBox.Show("success", MessageType.Success, modal: true, layout: MessageAlignment.Center);
            }
            else
            {
                return MessageBox.Show("don not success", MessageType.Error, modal: true, layout: MessageAlignment.Center);
            }
        }
        return Json(true);
    }

Script of jqgrid:

<script type="text/javascript">
    $(document).ready(function () {
        $('#list').jqGrid({
            caption: "State Info",
            url: '@Url.Action("GetState", "LocationGrid")',
            editurl: '@Url.Action("EditState", "LocationGrid")',
            datatype: 'json',
            jsonReader: {
                root: "Rows",
                page: "Page",
                total: "Total",
                records: "Records",
                repeatitems: true,
                id: "StateID",
                cell: "RowCells"
            },
            mtype: 'POST',
            colNames: ['ID','state','UserIDCreate_FK','UserIDChange_FK','StateDateCreate','StateDateChange', ''],
            colModel: [
                {
                    name: 'StateID', index: 'StateID',
                    editable: false, hidden: true, key: true
                },
                {
                    name: 'StateName', index: 'StateName', align: 'center', width: 120,
                    search: true, stype: 'text', searchoptions: { sopt: searchOptions },
                    editable: true, edittype: 'text',
                    editoptions: {
                        maxlength: 40
                    },
                    editrules: {
                        required: true
                    }
                },
                {
                    name: 'UserIDCreate_FK', index: 'UserIDCreate_FK', align: 'center', width: 80,
                    search: true, stype: 'text', searchoptions: { sopt: searchOptions },
                    editable: true, editoptions: { readonly: 'readonly' }
                },
            {
                name: 'UserIDChange_FK', index: 'UserIDChange_FK', align: 'center', width: 80,
                search: true, stype: 'text', searchoptions: { sopt: searchOptions },
                editable: false, edittype: 'text',
                editoptions: {
                    maxlength: 40
                },
                editrules: {
                    required: true
                }
            }
            ,
            {
                name: 'StateDateCreate', index: 'StateDateCreate', align: 'center', width: 135,
                search: true, stype: 'text', searchoptions: { sopt: searchOptions },
                editable: true, editoptions: { readonly: 'readonly' }
            },
            {
                name: 'StateDateChange', index: 'StateDateChange', align: 'center', width: 135,
                search: true, stype: 'text', searchoptions: { sopt: searchOptions },
                editable: false, edittype: 'text',
                editoptions: {
                    maxlength: 40
                },
                editrules: {
                    required: true
                }
            },
            {
                name: 'myac', width: 80, fixed: true, sortable: false, search: false,
                resize: false, formatter: 'actions',
                formatoptions: {
                    keys: true
                }
            }
            ],
            rowNum: 10,
            rowList: [10, 20, 50, 100],
            sortname: 'StateID',
            sortorder: 'desc',
            viewrecords: true,
            altRows: true,
            shrinkToFit: false,
            width: '900',
            height: 'auto',
            hidegrid: false,
            direction: "rtl",
            gridview: true,
            rownumbers: true,
            footerrow: true,
            loadComplete: function () {
                $("tr.jqgrow:odd").css("background", "#E0E0E0");
            },
            loadError: function (xhr, st, err) {
                jQuery("#rsperror").html("Type: " + st + "; Response: " + xhr.status + " " + xhr.statusText);
            }
        })
        ;
    });

Bellow picture show that UserIDCreate_FK is null but in grid this feild has value.

enter image description here

mahdis dezfouli
  • 173
  • 3
  • 19
  • One have many options to fix the problem. It could be important to know **which version of jqGrid you use**. The most simply way would be to use `editable: true, editoptions: { readonly: 'readonly' }` properties for `UserIDChange_FK` column. If it's not good in your scenario then you can use another implementation, but one require to know which fork jqGrid ([free jqGrid](https://github.com/free-jqgrid/jqGrid), [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or old jqGrid in version <=4.7) you use and in which version. – Oleg Sep 14 '15 at 11:10
  • One more remark. You use **mvcjqgrid** tag for the question which means that you use one special html helper for ASP.NET MVC (see [here](http://stackoverflow.com/tags/mvcjqgrid/info)). Do you really use the helper? If you don't use it then you should remove the tag **mvcjqgrid** from your question. If you use [**free jqGrid**](https://github.com/free-jqgrid/jqGrid) then you should add [free-jqgrid](http://stackoverflow.com/tags/free-jqgrid/info) tag to your question. – Oleg Sep 14 '15 at 11:14

0 Answers0