سؤال

Hi I started with JQgrid in MVC and i am performing CRUD operation in JQgrid, when i load my page the grid will filled with required data, but when i delete or edit, i am getting error, when i checked it, the error was the user id which is expected by edit action is not been initialized from view. My View Looks Like this

   <table id="jQGridDemo">
    </table>
    <div id="jQGridDemoPager">
    </div>
    <script type="text/javascript">
        jQuery("#jQGridDemo").jqGrid({
            url: 'RTGUser/GetUserDetails',

            datatype: "json",
            colNames: ['Id', 'Name', 'Designation', 'City'],
            colModel: [
             { name: 'Id', index: 'Id', width: 40, align: 'center', sortable: true, editable: false, key: true, editrules: { required: true} },
              { name: 'Name', index: 'Name', width: 40, align: 'center', sortable: true, editable: true, edittype: 'text', editrules: { required: true} },
              { name: 'Designation', index: 'Designation', width: 400, align: 'center', sortable: true, editable: true, edittype: 'text', editrules: { required: true} },
              { name: 'City', index: 'City', width: 150, align: 'center', sortable: true, editable: true, edittype: 'text', editrules: { required: true} }
              ],

            mtype: 'GET',
            loadonce: true,
            rowList: [5, 10, 20, 30],
            pager: '#jQGridDemoPager',
            sortname: 'Id',
            viewrecords: true,
            sortorder: 'desc',
            caption: "List Of Users",
             editurl: "RTGUser/EditUser",
            onSelectRow: function(Id){
        if(id && id!==lastsel){
            jQuery('#rowed3').jqGrid('restoreRow',lastsel);
            jQuery('#rowed3').jqGrid('editRow',Id,true);
            lastsel=Id;
        }
    },


        });

        jQuery("#jQGridDemo").jqGrid('navGrid', '#jQGridDemoPager',
      { edit: true, add: true, del: true, search: true },


           { url: "/RTGUser/EditUser/", closeAfterEdit: true, beforeShowForm: function (formid) { $("#Id",formid).hide(); } },
           { url: "/RTGUser/AddNewUser/", closeAfterAdd: true, beforeShowForm: function (formid) { $("#Id", formid).hide(); } },
           { url: "/RTGUser/DeleteUser/" }, {});

        $("#search").filterGrid("#grid", {
            gridModel: false,
            filterModel: [{
                label: 'Search',
                name: 'search',
                stype: 'text'
            }]
               });


    </script>

And My Controller Is

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult EditUser(string userId, string name, string desig, string city)
    {
        int uId = Convert.ToInt32(userId);
        try
        {
            var query = from u in db.Users
                        where u.Id.Equals(userId)
                        select u;
            User i = db.Users.SingleOrDefault(u => u.Id == uId);
            if (!(i == null))
            {
                var user = query.First();
                user.Name = name;
                user.Designation = desig;
                user.City = city;
                db.SaveChanges();


            }
            return Json(true);
        }
        catch (Exception)
        {
            // Do some error logging stuff, handle exception, etc.
            return Json(false);
        }
    }

What should i do to get mt id from my View. Thanks in Advance

هل كانت مفيدة؟

المحلول

Change your action signature to:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditUser(string Id, string Name, string Designation, string City)
{
} 

There's no magical way to attach values to your parameters, the binder will simply match them by names.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top