Jqgrid无法在我的SQL Server实例中添加,编辑或删除,我什至认为它甚至没有达到添加,编辑或删除功能

时间:2019-05-02 17:49:32

标签: c# sql-server asp.net-mvc entity-framework free-jqgrid

我在使用Free Jqgrid时遇到问题,无法将其发布到我的Microsoft SQL Server开发人员版本中。我正在使用Microsoft Visual Studio。我正在使用实体框架6进行数据库连接。当我使用断点时,似乎没有达到我的createbid,editbid或delete bid。在控制台上也没有任何错误。

我已经阅读并完成了许多有关编辑更新和添加行的教程。我有同样的问题。通过我的研究,我找不到适用于堆栈溢出的答案。

我将其发布到IIS,以查看是否与视觉工作室有关,并查看是否有任何更改,但是这次确实有一些错误。找不到/ Home / GetBidValues,Home / EditBid,/ Home / CreateBid,/ Home / DeleteBid。这很奇怪,因为对于视觉工作室而言,至少是将数据放入网格并进行显示。因此,我将进一步研究这个问题,看看我的ajax和C#代码是否有问题。

这是我的Jquery文件

$(function () {
    $("#grid").jqGrid
        ({
            url: "/Home/GetBidValues",
            datatype: 'json',
            mtype: 'Get',  
            colNames: ['Client Cost', 'ElementId', 'Note', 'Area', 'Element', 'Item', 'Qty', 'Description'], //Othr Misc
            //colModel takes the data from controller and binds to grid   
            colModel: [
               {
                    key: true,
                    hidden: true,
                    name: 'ElementId',
                    index: 'ElementId',
                    frozen: true,
                    editable: false
                }, {
                    key: false,
                    name: 'Note',
                    index: 'Note',
                    frozen: true,
                    editable: true,
                    width: 70,
                    align: 'center'
                }, {
                    key: false,
                    name: 'Area',
                    index: 'Area',
                    frozen: true,
                    editable: true,
                    width: 70,
                    align: 'center'
                }, {
                    key: false,
                    name: 'Element',
                    index: 'Element',
                    frozen: true,
                    editable: true,
                    width: 70,
                    align: 'center'
                }, {
                    name: 'Item',
                    index: 'Item',
                    frozen: true,
                    editable: true,
                    width: 70,
                    align: 'center'
                }, {
                    key: false,
                    name: 'QTY',
                    index: 'QTY',
                    frozen: true,
                    editable: true,
                    width: 70,
                    align: 'center'
                }, {
                    key: false,
                    name: 'Description',
                    index: 'Description',
                    frozen: true,
                    editable: true,
                    width: 70,
                    align: 'center'
                }],

            pager: '#pager',
            gridview: true,
            ignoreCase: true,
            rowNumbers: false,
            shrinkToFit: false,
            height: '100%',
            width: 1000,
            viewrecords: true,
            footerrow: true,
            emptyrecords: 'No records to display',
            jsonReader:
            {
                root: "rows",
                page: "page",
                total: "total",
                records: "records",
                repeatitems: false,
                Id: "0"
            }

        });
        $('#grid').jqGrid( 'inlineNav','#pager', {
            edit: true,
            add: true,
            del: true,
            cancel: true,
            search: true,
            refresh: true,
            editparams: {
                keys:true
            }
        }, {
            // edit options  
            zIndex: 100,
                url: '/Home/EditBid',
                //datatype: 'json',
                //mtype: 'Post',
            closeOnEscape: true,
            closeAfterEdit: true,
            recreateForm: true,
            reloadaftersubmit: true,
            afterComplete: function (response) {
                if (response.responseText) {
                    alert(response.responseText);
                }
            }
        },{//add
            zIndex: 100,
                url: "/Home/CreateBid",
            closeOnEscape: true,
                closeAfterAdd: true,
                reloadaftersubmit: true,
            afterComplete: function (response) {
                if (response.responseText) {
                    alert(response.responseText);
                }
            }
        }, {
            // delete options  
            zIndex: 100,
                url: "/Home/DeleteBid",
            closeOnEscape: true,
            closeAfterDelete: true,
                recreateForm: true,
                reloadaftersubmit: true,
            msg: "Are you sure you want to delete this task?",
            afterComplete: function (response) {
                if (response.responseText) {
                    alert(response.responseText);
                }
            }
        });
});

这是我的C#文件

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using ERPWebAppTest.Models;
using Microsoft.AspNet.Identity;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Owin;


namespace ERPWebAppTest.Controllers
{
    public class HomeController : Controller
    {       
        public ActionResult Index()
        {
            return View();
        }

        Test1Entities db = new Test1Entities();

        [HttpGet]
        public JsonResult GetBidValues(string sidx, string sord, int page, int rows) 
        {
            //old code
            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            var Results = db.BidDetails.Select(
                a => new
                {
                    a.ElementID,
                    a.Note ,
                    a.Area ,
                    a.Element ,
                    a.Item ,
                    a.QTY ,
                    a.Descr ,        
    });
            int totalRecords = Results.Count();
            var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
            if (sord.ToUpper() == "DESC")
            {
                Results = Results.OrderByDescending(s => s.ElementID);
                Results = Results.Skip(pageIndex * pageSize).Take(pageSize);
            }
            else
            {
                Results = Results.OrderBy(s => s.ElementID);
                Results = Results.Skip(pageIndex * pageSize).Take(pageSize);
            }
            var jsonData = new
            {
                total = totalPages,
                page,
                records = totalRecords,
                rows = Results
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public string CreateBid([Bind(Exclude = "ElementID")] BidDetail obj)
        {


            Test1Entities db = new Test1Entities();
            string msg;
            try
            {
                if (ModelState.IsValid)
                {
                    db.BidDetails.Add(obj);
                    db.SaveChanges();
                    msg = "Saved Successfully";
                }
                else
                {
                    msg = "Validation data not successfully";
                }
            }
            catch (Exception ex)
            {
                msg = "Error occured:" + ex.Message;
            }

            return msg;


        }
        [HttpPost]
        public string EditBid(BidDetail obj)
        {
            Test1Entities db = new Test1Entities();
            string msg;

            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(obj).State = EntityState.Modified;
                    db.SaveChanges();
                    msg = "Saved Successfully";
                }
                else
                {
                    msg = "Validation data not successfull";
                }
            }
            catch (Exception ex)
            {
                msg = "Error occured:" + ex.Message;
            }

            return msg;
        }


        [HttpPost]
        public string DeleteBid(int ElementID)
        {
            Test1Entities db = new Test1Entities();
            BidDetail list = db.BidDetails.Find(ElementID);
            db.BidDetails.Remove(list);
            db.SaveChanges();

            return "Deleted successfully";
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我在您的jqgrid代码中看到许多小问题,这可能是当前问题的原因之一。您可以更改它们以使上面的代码可行。

  • colModel项目编号与colNames项目不完全相同。您最初是在colNames中编写Client Cost的,但在colModel中却没有使用它。
  • 您还没有粘贴指向jqgrid加载到表格元素上所需的html代码。 例如: <table id="grid" class="table table-bordered"></table> <div id="pager"></div>
  • 我还假设您已经添加了free-jqgrid脚本和jquery-ui css文件。
  • 此外,在您的C#代码中,您可以使用全局db初始化来消除冗余。

我创建了一个演示jqgrid,您可以根据自己的需求进行更改:

<div class="row">

<table id="grid" class="table table-bordered"></table>
<div id="pager"></div>
</div>

@section Scripts{
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/Scripts/free-jqGrid/jquery.jqgrid.min.js"></script>
<script type="text/javascript">
    $(function () {
        $("#grid").jqGrid
            ({
                url: "/Home/GetBidValues",
                datatype: 'json',
                mtype: 'Get',
                colNames: ['LanguageId', 'LanguageName', 'LanguageDescription', 'CreatedBy', 'CreatedOn'], //Othr Misc
                //colModel takes the data from controller and binds to grid
                colModel: [
                    //{ name: "act", template: "actions", align: "left", width: 58 },
                   {
                       key: true,
                       hidden: true,
                       name: 'LanguageId',
                       index: 'LanguageId',

                       editable: false
                   }, {
                       key: false,
                       name: 'LanguageName',
                       index: 'LanguageName',
                       frozen: true,
                       editable: true,

                       align: 'center'
                   }, {
                       key: false,
                       name: 'LanguageDescription',
                       index: 'LanguageDescription',

                       editable: true,

                       align: 'center'
                   }, {
                       key: false,
                       name: 'CreatedBy',
                       index: 'CreatedBy',

                       editable: true,

                       align: 'center'
                   }, {
                       name: 'CreatedOn',
                       index: 'CreatedOn',

                       editable: true,

                       align: 'center'
                   }],
                //cmTemplate: { editable: true, autoResizable: true },
                iconSet: "fontAwesome",
                rowNum: 10,
                //autoResizing: { compact: true },
                rowList: [5, 10, 20, "10000:All"],
                viewrecords: true,
                pager: jQuery('#pager'),
                toppager: false,
                inlineEditing: { keys: true, position: "afterSelected" },
                rownumbers: true,
                sortname: "invdate",
                sortorder: "desc",
                caption: "Demonstration of the usage custom action buttons",
                autowidth: true,
                shrinkToFit: false,
                jsonReader:
                {
                    root: "rows",
                    page: "page",
                    total: "total",
                    records: "records",
                    repeatitems: false,
                    Id: "LanguageId"
                }

            });
        jQuery("#grid").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: 
false, defaultSearch: "cn", multipleSearch: true, searchOperators: true, search: true 
});        //searchOnEnter: false means records are filtered as soon as the text is 
entered by the user
        jQuery("#grid").jqGrid('navGrid', '#pager', { add: true, edit: true, del: true, search: true, refresh: true,view:true }, {
            // edit options
            zIndex: 100,
            url: '/Home/EditBid',
            //datatype: 'json',
            //mtype: 'Post',
            closeOnEscape: true,
            closeAfterEdit: true,
            recreateForm: true,
            reloadaftersubmit: true,
            afterComplete: function (response) {
                if (response.responseText) {
                    alert(response.responseText);
                }
            }
        }, {//add
            zIndex: 100,
            url: "/Home/CreateBid",
            closeOnEscape: true,
            closeAfterAdd: true,
            reloadaftersubmit: true,
            afterComplete: function (response) {
                if (response.responseText) {
                    alert(response.responseText);
                }
            }
        }, {
            // delete options
            zIndex: 100,
            url: "/Home/DeleteBid",
            closeOnEscape: true,
            closeAfterDelete: true,
            recreateForm: true,
            reloadaftersubmit: true,
            msg: "Are you sure you want to delete this task?",
            afterComplete: function (response) {
                if (response.responseText) {
                    alert(response.responseText);
                }
            }
        });
    });
</script>

}