我正在使用jqGrid开发Asp.Net MVC5应用程序。 我有两个模型大学和宗教。
public class University
{
public int UniversityID { get; set; }
public string UniversityName { get; set; }
}
public class Religion
{
public int ReligionID { get; set; }
public string ReligionName { get; set; }
}
我有一个名为Student的模型,其中嵌套了上述两个类。
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public DateTime DOB { get; set; }
public string Gender { get; set; }
public University University { get; set; }
public Religion Religion { get; set; }
}
我在jqGrid中填写了学生名单。
//jqGrid binding through ajax Post
var jsonUnivList = $.parseJSON('@Html.Raw(Json.Encode(Model.Universities))'); //IEnumerable list of Universities
var jsonReligionList = $.parseJSON('@Html.Raw(Json.Encode(Model.Religions))'); // IEnumerable list of Religion
$("#list2").jqGrid({
url: '/Student/StudentGridData',
datatype: "json",
colNames: ['Student Id', 'Student Name', 'Gender', 'DOB', 'University', 'Religion'],
colModel: [
{ name: 'StudentId', index: 'StudentId', width: 70, hidden: true },
{ name: 'StudentName', index: 'StudentName', width: 130, sortable: true, editable: true, formoptions: { label: 'Name *' }, editoptions: { class: "validate[required]", "data-errormessage-value-missing": "*Name Required", "onblur": "$(this).validationEngine('validate');" } },
{
name: 'Gender', index: 'Gender', width: 80, align: "right", sortable: true, editable: true, edittype: 'select',
editoptions:
{
value: { '': '--select gender--', 'M': 'MALE', 'F': 'FEMALE' }
}
},
{ name: 'DOB', index: 'DOB', formatter: 'date', formatoptions: { srcformat: 'd/m/Y', newformat: 'ShortDate' }, width: 150, align: "right", sortable: true, editable: true, formoptions: { label: 'DOB *' }, editoptions: { class: "validate[required]", "data-errormessage-value-missing": "*DOB Required", "onblur": "$(this).validationEngine('validate');" } },
{
name: 'University.UniversityName', index: 'University.UniversityName', width: 150, align: "right", sortable: true, editable: true, edittype: 'select', formoptions: { label: 'Name *' },
editoptions:
{
dataUrl: '',
buildSelect: function (data) {
var s = '<select name="UniversityID" >';
if (jsonUnivList && jsonUnivList.length) {
for (var i = 0, l = jsonUnivList.length; i < l ; i++) {
s += '<option value="' + jsonUnivList[i].UniversityID + '">' + jsonUnivList[i].UniversityName + '</option>';
}
}
return s + "</select>";
},
class: "validate[required]", "data-errormessage-value-missing": "*University Required", "onblur": "$(this).validationEngine('validate');"
}
},
{
name: 'Religion.ReligionName', index: 'Religion.ReligionName', width: 150, align: "right", sortable: true, editable: true, edittype: 'select', formoptions: { label: 'Name *' },
editoptions:
{
dataUrl: '',
buildSelect: function (data) {
var s = '<select name= "ReligionID">';
if (jsonReligionList && jsonReligionList.length) {
for (var i = 0, l = jsonReligionList.length; i < l ; i++) {
s += '<option value="' + jsonReligionList[i].ReligionID + '">' + jsonReligionList[i].ReligionName + '</option>';
}
}
return s + "</select>";
},
class: "validate[required]", "data-errormessage-value-missing": "*Religion Required", "onblur": "$(this).validationEngine('validate');"
}
}
],
rowNum: 10,
rowList: [10, 20, 30],
pager: '#pager2',
sortname: 'StudentId',
mtype: 'POST',
viewrecords: true,
sortorder: "desc",
caption: "Student List",
editurl: '/Student/AddEditStudent'
});
$("#list2").jqGrid('navGrid', '#pager2',
{
edit: true, add: true, del: true, search: true,
searchtext: "Search", addtext: "Add", edittext: "Edit", deltext: "Delete"
}
);
点击了jqGrid的Add选项并填写了详细信息(Uiversity and Religion是下拉列表 - 学生的嵌套模型)。填充的数据将传递给Controller。
[HttpPost]
public void AddEditStudent(Student StudentModel)
{
}
但是当我检查控制器收到的数据时, 我收到以下数据: 学生姓名 DOB 性别
但这些都是空的: 大学 宗教
注意:此问题仅出现在jqGrid的情况下, 如果我从页面提交(相同的文本框和下拉列表),那么一切正常。
答案 0 :(得分:0)
这就是我对学生对象进行建模的方法:
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public DateTime DOB { get; set; }
public string Gender { get; set; }
public int UniversityID { get; set; }
public int ReligionID { get; set; }
}
在JQ Grid中,我以与您的方式略有不同的方式绑定选项。这就是我将选项绑定到大学专栏的方式:
editoptions:&#34; 1:斯坦福; 2:哈佛; 3:耶鲁&#34;
现在保存时,所选选项的ID将映射到Student对象的相应属性 (我假设你使用jqGrid的saveRow函数来提交数据。)
答案 1 :(得分:0)
最后我想出来了
{
name: 'University.UniversityID', index: 'University.UniversityName', jsonmap: "University.UniversityName", width: 150, align: "right", sortable: true, editable: true, edittype: 'select', formoptions: { label: 'University *', name: "University.UniversityID" },
editoptions:
{
value: valUnivList, // List of university as keyvalue pair
class: "validate[required]", "data-errormessage-value-missing": "*University Required", "onblur": "$(this).validationEngine('validate');"
}
}
这是大学的样本colModel, 这里我们要提交给控制器的字段应该在name属性中给出, 并且应在jsonmap属性中给出要在网格中显示的字段。