JQGrid使用下拉菜单编辑外键值

时间:2018-07-25 10:30:37

标签: javascript jquery asp.net-mvc asp.net-core jqgrid

我在.net core 2.0项目中有一个用于状态类的JQGrid,该类具有Country类的外键引用。但是,当我保存countryId列时始终为0。

 <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> 
</script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<script type="text/javascript">
   jQuery.browser = {};
   (function () {
          jQuery.browser.msie = false;
          jQuery.browser.version = 0;
          if (navigator.userAgent.match(/MSIE ([0-9]+)\./)) {
                 jQuery.browser.msie = true;
                 jQuery.browser.version = RegExp.$1;
          }
   })();
</script>
<script src="~/js/grid.locale-en.js"></script>
<script src="~/js/jquery.jqGrid.min.js"></script>

   <link href="~/css/ui.jqgrid-bootstrap4.css" rel="stylesheet" />
   <link href="~/css/ui.jqgrid-bootstrap-ui.css" rel="stylesheet" />*@
 <link href="~/css/jquery-ui.css" rel="stylesheet" />
<link href="~/css/ui.jqgrid.css" rel="stylesheet" />
<table id="list-student" style="color:black !important" class="table table-dark"></table>
<div id="pager"></div>
<script>      
var data = $.ajax({
              url: '/Admin/GetAllCountries', async: false, success: function (data, result) { if (!result) alert('Failure to retrieve the Alert related lookup data.'); }
   }).responseJSON;
   var lookupData = data;
   var listingCountry = lookupData;
   debugger;
   var catList = '{';
   $(lookupData).each(function () {
          debugger;
          catList += this.countryid + ':' + this.countryName + ';';
   });
   catList += '}';

   $('#list-student').jqGrid({
          url: '/Admin/GetState',
          datatype: 'json',
          colNames: ['Id', 'Country', 'State', 'Status'],
          colModel: [
                 { name: 'id', index: 'Id', editable: false, width: 100, key: true, hidden: true },                   
                 { name: 'country.countryName', index: 'country.countryid ', editable: true, width: 100, hidden: false, edittype: "select", type: 'select',  editrules: { required: true }, editoptions: { value: catList } },
                 { name: 'name', index: 'name', editable: true, width: 300 },
                 { name: 'status', index: 'status', editable: false, width: 240, hidden: true }

          ],
          jsonReader: {
                 root: "rows",
                 page: "page",
                 total: "total",
                 records: "records",
                 repeatitems: false,
                 id: "0"
          },
          rowNum: 10,
          rowList: [1, 2, 3],
          pager: '#pager',
          sortname: 'name',
          sortorder: 'asc',
          footerrow: true,
          viewrecords: true,
          height: 'auto',
          width: 'auto',
          sortorder: 'asc',
          loadonce: false,
          shrinkToFit: true,
          emptyrecords: 'No records',
          caption: 'List of States',
          grouping: true,
          loadComplete: function () {

          },
          //groupingView: { groupField: ['countryId'], groupSummary: [true], groupColumnShow: [true], groupText: ['<b>{0}</b>'], groupCollapse: true, groupOrder: ['asc'] },
   }).navGrid('#pager', { edit: true, add: true, del: true, search: true, refresh: true, closeAfterSearch: true },
          {
                 zIndex: 100,
                 url: '/Admin/EditState',
                 closeOnEscape: true,
                 closeAfterEdit: true,
                 recreateForm: true,
                 afterComplete: function (response) {
                       if (response.responseText) {
                              alert(response.responseText);
                       }
                 }
          },
          {

                 url: "/Admin/CreateState",
                 closeOnEscape: true,
                 closeAfterAdd: true,
                 afterComplete: function (response) {
                       if (response.responseText) {
                              alert(response.responseText);
                       }
                 }
          },
          {
                 zIndex: 100,
                 url: "/Student/Delete",
                 closeOnEscape: true,
                 closeAfterDelete: true,
                 recreateForm: true,
                 msg: "Are you sure you want to delete Student... ? ",
                 afterComplete: function (response) {
                       if (response.responseText) {
                              alert(response.responseText);
                       }
                 }
          });  
</script>

该国家/地区的下拉菜单工作正常。这是下拉菜单的屏幕截图。它正确地绑定了数据Country DropDown

0 个答案:

没有答案