单击按钮时未加载页面但在MVC中的jqGrid中的链接按钮上工作

时间:2017-12-27 05:12:18

标签: model-view-controller jqgrid

我有一个页面(Steps_Details.cshtml),MVC中有一个jqgrid,我希望以3种方式调用它,它根据我作为参数发送的Unique_code加载数据。第一种方式)我有另一个页面(Rep_Contracts_Steps.cshtml)与jqgrid,我有一个链接按钮,通过单击此链接按钮并正确发送参数数据。第二种方式)主菜单上没有参数的链接,它加载所有数据,没有过滤。 第三种方式和我遇到的问题)我在页面中有一个按钮,我发送参数。它指向视图但不加载页面。(事实上,当我使用btnRep时,它不会在我的HomeController中调用“Get_Contracts_Steps_Details”动作)

“Steps_Details.cshtml”

    <script type="text/javascript">
$(document).ready(function () {
    $('#list').jqGrid({
        caption: "گزارش جزییات پیگیری قراردادها",
        url: '@Url.Action("Get_Contracts_Steps_Details","Home")',
        datatype: 'json',
        jsonReader: {
            root: "Rows",
            page: "Page",
            total: "Total",
            records: "Records",
            repeatitems: true,
            userdata: "UserData",
            id: "Vahed_Descript",
            cell: "RowCells"
        },
        mtype: 'GET',
        //columns names
        colNames: [' ردیف ',
                    ' کد تایید ',
                    ' عنوان تایید ',
                    ' کاربر تایید کننده ',
                    ' تاریخ تایید ',
                    ' توضیحات',
                    ' کاربر ثبت کننده',
                    ' تاریخ ثبت',
                    ' کاربر ویرایش کننده' , 
                    ' تاریخ ویرایش' ,
                    ' عنوان واحد ',
                    ' کد دبیرخانه ',
                    ' شماره درخواست ',
                    ' شرح درخواست ',
                    ' تاریخ ثبت درخواست',
                    ' مبلغ برآوردی ',
                     '' 
        ],
        //columns model

        colModel: [
            { name: 'MCS_ID', index: 'MCS_ID', align: 'Right', width: "60px", editable: true, editoptions: { readonly: "readonly" } },
            { name: 'MCS_ConfirmId', index: 'MCS_ConfirmId', align: 'Right', width: "60px", editable: false },
            { name: 'MWL_Descript', index: 'MWL_Descript', align: 'Right', editable: false },
            { name: 'MCS_ConfirmUser', index: 'MCS_ConfirmUser', align: 'Right', editable: true, edittype: 'text' },
            { name: 'MCS_ConfirmDate', index: 'MCS_ConfirmDate', align: 'Right', width: "80px",  editable: true, edittype: 'text',
                  editrules: {  required: true   }
            },
            { name: 'MCS_Note', index: 'MCS_Note', align: 'Right', width: "300px", editable: true, edittype: 'text' },
            { name: 'MCS_CreatorUserName', index: 'MCS_CreatorUser', align: 'Right', editable: false },
            { name: 'MCS_Creation_Date', index: 'MCS_Creation_Date', align: 'Right', width: "80px", editable: false },
            { name: 'MCS_EditorUserName', index: 'MCS_EditorUserName', align: 'Right', editable: false },
            { name: 'MCS_EditDate', index: 'MCS_EditDate', align: 'Right', width: "80px", editable: false },
            { name: 'DESCRIPT', index: 'DESCRIPT', align: 'right', editable: false },
            { name: 'DABIR_VAHED_CODE', index: 'DABIR_VAHED_CODE', align: 'right', width: "60px", editable: false },
            { name: 'CONTRACT_REQ_ID', index: 'CONTRACT_REQ_ID', align: 'right', width: "100px", editable: false },
            { name: 'CONTRACT_REQ_SUBJECT', index: 'CONTRACT_REQ_SUBJECT', align: 'right', editable: false },
            { name: 'D_CONTRACT_REQ', index: 'D_CONTRACT_REQ', align: 'right', width: "100px", editable: false },
            { name: 'AMNT_TOT_ESTIMATE', index: 'AMNT_TOT_ESTIMATE', align: 'right', width: "100px", editable: false,
                formatter: 'currency',   formatoptions:
                {  decimalSeparator: '.', thousandsSeparator: ',', decimalPlaces: 0 }
            },
            { name: 'MCS_REQ_UNIQUE', index: 'MCS_REQ_UNIQUE', width: 1, align: 'left', hidden: true },

       ],

        pager: $('#pager'),
        rowNum: 200,
        rowList: [10, 20, 40, 60, 80, 100, 150, 200, 300],
        sortname: 'CONTRACT_REQ_SUBJECT',
        sortorder: 'asc',
        viewrecords: true,
        altRows: true,
        shrinkToFit: false,
        width: 'auto',
        height: '300px',
        hidegrid: false,
        direction: "rtl",
        gridview: true,
        rownumbers: true,
        footerrow: true,
        userDataOnFooter: true,
        scrollerbar: true,
        loadComplete: function () {
            $("tr.jqgrow:odd").css("background", "#E0E0E0");
        },
        loadError: function (xhr, st, err) {
            jQuery("#rsperror").html("Type: " + st + "; Response: " + xhr.status + " " + xhr.statusText);
        }
    , loadonce: true

    })

      .jqGrid('navGrid', "#pager",
       //enabling buttons
        { add: false, del: true, edit: true, search: true, refresh: true },
        //edit option
        {
            url: '@Url.Action("EditSteps_Details", "Home")' ,
            width: 'auto',
            checkOnUpdate: true,
            checkOnSubmit: true,
            closeOnEscape: true,
            closeAfterEdit: true, 
            beforeShowForm: function (form) {
                centerDialog(form, $('#list'));
            },
            afterSubmit: function (response, postdata) {

                $("#list").jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid');

                return [true, "", ''];

            },

             afterComplete: function (response) {  
               if (response.responseText) {  
                alert(response.responseText);  
              }  
       }  

        },

        //delete options
        {
            url: '@Url.Action("DeleteSteps_Details", "Home")', reloadAfterSubmit: false
            , beforeShowForm: function (form) {
                 centerDialog(form, $('#list'));
            },
        });



    function centerDialog(form, grid) {
        var dlgDiv = $("#editmod" + grid[0].id);
        var parentDiv = dlgDiv.parent(); // div#gbox_list
        var dlgWidth = dlgDiv.width();
        var parentWidth = parentDiv.width();
        var dlgHeight = dlgDiv.height();
        var parentHeight = parentDiv.height();
        var parentTop = parentDiv.offset().top;
        var parentLeft = parentDiv.offset().left;
        dlgDiv[0].style.top = Math.round(parentTop + (parentHeight - dlgHeight) / 2) + "px";
        dlgDiv[0].style.left = Math.round(parentLeft + (parentWidth - dlgWidth) / 2) + "px";
    }




    function toolbarSearching() {
        $("#list").jqGrid('filterToolbar', {
            autosearch: true,
            stringResult: false,
            searchOnEnter: true,
            defaultSearch: "cn",
        });
    };



    $("#list").jqGrid('filterToolbar', {
        autosearch: true,
        stringResult: false,
        searchOnEnter: true,
        defaultSearch: "cn",
    });

    var maxNameLength = 10;
    $("input[id=gs_name]").blur(function () {
        var $th = $(this).closest(".ui-search-toolbar>th"),
            colIndex = $th[0].cellIndex,
            $colHeader = $th.parent().siblings(".ui-jqgrid-labels").children("th").eq(colIndex),
            colHeaderText = $colHeader.children("div").text();
        if (this.value.length > maxNameLength) {
            alert(colHeaderText + ' is longer than ' + maxNameLength + ' characters.');
        }
    });
    $('#gs_invdate').datepicker({
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true
    });


})

    </script>

“Steps_Details”行动

 [HttpGet]
    public ActionResult Steps_Details(string REQ_UNIQE)
    {
        if (Session["LoginName"] == null)
        { 
            return RedirectToAction("Index", "Home");
        }
       else
        {
            if (REQ_UNIQE != null)
            {

                if (REQ_UNIQE != "")
                {
                    Session["REQ_UNIQE"] = REQ_UNIQE.ToString();
                }
            }
              return View(); 
        }
    }

“按钮和我将其值作为参数”

发送的下拉列表
   <div class="form-group">
@Html.DropDownListFor(model => model.Req_Unique, (SelectList)ViewBag.Contract_Req_Sub_List, "انتخاب", new { @class = "form-control", @id = "ddlContrReqSub", @onchange = "javascript:GetWork_Level(this.value);" })
   <input type="button"  name="submit" value="گزارش" id="btnRep" class="w3-button w3-MyDarkBlue w3-padding-large w3-margin-bottom" />
      </div>

“btnRep的javascript代码”

    $(document).ready(function () {
            $("#btnRep").click(function () {
                var REQ_UNIQE = $("#ddlContrReqSub").val();

                $.ajax({
                    url: "/Home/Steps_Details",
                    type: "GET",
                    data: { REQ_UNIQE: REQ_UNIQE  },

                });
            });
        }); 

“HomeController中的Get_Contracts_Steps_Details操作”

   [HttpGet]
    public ActionResult Get_Contracts_Steps_Details(JqGridRequest request )
    { 
        using (rw_mis_dbEntities dc = new rw_mis_dbEntities())
        { 

           if (Session["REQ_UNIQE"]  != null )
            {
          var  list = dc.MVC_REP_Contracts_Steps_Details_By_Req_Unique(Security.SharedMembers.shrd_Year_Abbr, Security.SharedMembers.shrd_Vahed_Id, Security.SharedMembers.shrd_User_Id, "", "", 0, new Guid(Session["REQ_UNIQE"].ToString())).ToList();

          var pageIndex = request.page - 1;
          var pageSize = request.rows;
          var totalRecords = list.Count;
          var totalPages = (int)Math.Ceiling(totalRecords / (float)pageSize);


          var contractsQuery = list.AsQueryable();

          contractsQuery = new JqGridSearch().ApplyFilter(contractsQuery, request._search, request.searchField, request.searchString,
                                                         request.searchOper, request.filters, this.Request.Form);


          if (string.IsNullOrWhiteSpace(request.oper))
          {
              contractsQuery = contractsQuery
                                  .Skip(pageIndex * pageSize)
                                  .Take(pageSize);
          }
          else if (request.oper == "excel")
          {
              contractsQuery = contractsQuery
                                  .Skip(pageIndex * pageSize);
          }

          var contractsList = contractsQuery
                                .OrderBy(request.sidx + " " + request.sord)
                                .Skip(pageIndex * pageSize)
                                .Take(pageSize)
                                .ToList();

          var jqGridData = new JqGridData
          {
              UserData = new // نمايش در فوتر
              {
                  Name = "جمع صفحه"
              },
              Total = totalPages,
              Page = request.page,
              Records = totalRecords,
              Rows = (contractsList.Select(Contract => new JqGridRowData
              {
                  Id = Convert.ToInt32(Contract.MCS_ID),
                  RowCells = new List<string>
                                                {   Contract.MCS_ID.ToString(), 
                                                    Contract.MCS_ConfirmId.ToString(),
                                                    Contract.MWL_Descript,
                                                    Contract.MCS_ConfirmUser,
                                                    Contract.MCS_ConfirmDate.ToString() ,
                                                    Contract.MCS_Note ,
                                                    Contract.MCS_CreatorUserName.ToString() ,
                                                    Contract.MCS_CreationDate.ToString() ,
                                                    Contract.MCS_EditorUserName.ToString() ,
                                                    Contract.MCS_EditDate .ToString() ,
                                                    Contract.VAHED_DESCRIPT ,
                                                    Contract.DABIR_VAHED_CODE, 
                                                    Contract.CONTRACT_REQ_ID ,
                                                    Contract.CONTRACT_REQ_SUBJECT ,
                                                    Contract.D_CONTRACT_REQ ,
                                                    Contract.AMNT_TOT_ESTIMATE.ToString() ,
                                                    Contract.MCS_REQ_UNIQUE.ToString ()
                                                  }
              })).ToList()
          };
          Session["REQ_UNIQE"] = null;
          return Json(jqGridData, JsonRequestBehavior.AllowGet); 


           }

              else 
              {
             var  list = dc.MVC_REP_Contracts_Steps_Details(Security.SharedMembers.shrd_Year_Abbr, Security.SharedMembers.shrd_Vahed_Id, Security.SharedMembers.shrd_User_Id, "", "", 0 ).ToList();


             var pageIndex = request.page - 1;
             var pageSize = request.rows;
             var totalRecords = list.Count;
             var totalPages = (int)Math.Ceiling(totalRecords / (float)pageSize);


             var contractsQuery = list.AsQueryable();

             contractsQuery = new JqGridSearch().ApplyFilter(contractsQuery, request._search, request.searchField, request.searchString,
                                                            request.searchOper, request.filters, this.Request.Form);


             if (string.IsNullOrWhiteSpace(request.oper))
             {
                 contractsQuery = contractsQuery
                                     .Skip(pageIndex * pageSize)
                                     .Take(pageSize);
             }
             else if (request.oper == "excel")
             {
                 contractsQuery = contractsQuery
                                     .Skip(pageIndex * pageSize);
             }

             var contractsList = contractsQuery
                                   .OrderBy(request.sidx + " " + request.sord)
                                   .Skip(pageIndex * pageSize)
                                   .Take(pageSize)
                                   .ToList();


             var jqGridData = new JqGridData
             {
                 UserData = new // نمايش در فوتر
                 {
                     Name = "جمع صفحه"
                 },
                 Total = totalPages,
                 Page = request.page,
                 Records = totalRecords,
                 Rows = (contractsList.Select(Contract => new JqGridRowData
                 {
                     Id = Convert.ToInt32(Contract.MCS_ID),
                     RowCells = new List<string>
                                                {   Contract.MCS_ID.ToString(), 
                                                    Contract.MCS_ConfirmId.ToString(),
                                                    Contract.MWL_Descript,
                                                    Contract.MCS_ConfirmUser,
                                                    Contract.MCS_ConfirmDate.ToString() ,
                                                    Contract.MCS_Note ,
                                                    Contract.MCS_CreatorUserName.ToString() ,
                                                    Contract.MCS_CreationDate.ToString() ,
                                                    Contract.MCS_EditorUserName.ToString() ,
                                                    Contract.MCS_EditDate .ToString() ,
                                                    Contract.VAHED_DESCRIPT ,
                                                    Contract.DABIR_VAHED_CODE, 
                                                    Contract.CONTRACT_REQ_ID ,
                                                    Contract.CONTRACT_REQ_SUBJECT ,
                                                    Contract.D_CONTRACT_REQ ,
                                                    Contract.AMNT_TOT_ESTIMATE.ToString() ,
                                                    Contract.MCS_REQ_UNIQUE.ToString ()
                                                  }
                 })).ToList()
             };
             return Json(jqGridData, JsonRequestBehavior.AllowGet); 

           } 

        } 
    }

提前致谢

2 个答案:

答案 0 :(得分:0)

方法Get_Contracts_Steps_Details的返回类型为ActionResult。但jqGrid定义为datatype:'json'

如果Get_Contracts_Steps_Details要返回jqGrid的json结果,请使用返回类型 string JsonResult

答案 1 :(得分:0)

我刚刚改变了

 <input type="button"  name="submit" value="گزارش" id="btnRep" class="w3-button w3-MyDarkBlue w3-padding-large w3-margin-bottom" />

 <input type="button" name="submit" value="گزارش" id="btnRep" class="w3-button w3-MyDarkBlue w3-padding-large w3-margin-bottom" onclick="location.href='@Url.Action("Steps_Details", "Home")?Req_Unique=' + $('#ddlcontrreqsub').val()" />   

并且有效