如何获取asp.net mvc站点中选择控件的jqGrid editmode中返回的值?
我有一个网格,它从jqGrid编辑表单中的dataurl填充选择列表。这些值在get上成功填充。我以json的形式提取数据,并且有一个自定义类型的LookupItem,其中包含在选择列表中显示的项的字段Id和名称——这部分工作正常。例如,我有一个表示计划课程的对象模型(在下面的第二个代码示例中),它有一个LookupItem类型的Location字段,LookupItem有Id和Name字段 我的问题是,虽然在我的ActionResult方法中,这些字段没有发回服务器-我得到了null。我添加了一个自定义的_func函数,并验证了value参数是否包含我要查找的Id,但我不确定如何将其发布到ActionResult方法。有什么想法吗 HTML/jqGrid代码:如何获取asp.net mvc站点中选择控件的jqGrid editmode中返回的值?,jqgrid,Jqgrid,我有一个网格,它从jqGrid编辑表单中的dataurl填充选择列表。这些值在get上成功填充。我以json的形式提取数据,并且有一个自定义类型的LookupItem,其中包含在选择列表中显示的项的字段Id和名称——这部分工作正常。例如,我有一个表示计划课程的对象模型(在下面的第二个代码示例中),它有一个LookupItem类型的Location字段,LookupItem有Id和Name字段 我的问题是,虽然在我的ActionResult方法中,这些字段没有发回服务器-我得到了null。我添加了
$("#grid").jqGrid({
url: 'ABC.Admin/CourseAdmin/GetScheduledCourses/',
datatype: 'json',
jsonReader: { repeatitems: false },
mtype: 'GET',
colNames: ['Id', 'Date', 'LocationId', 'Location', 'TimeId', 'Time', 'CostId', 'Cost', 'InstructorId', 'Instructor', 'Additional Info'],
colModel: [
{ name: 'ScheduledCourseId', index: 'ScheduledCourseId', width: 40, key: true, hidden: true },
{ name: 'ScheduledCourseDate', index: 'ScheduledCourseDate', width: 50, editable: true },
{ name: 'Location.Id', index: 'Location.Id', width: 25, editable: false, hidden: true },
{ name: 'Location.Name', index: 'Location.Name', width: 150, editable: true, edittype: 'select',
editoptions: {
dataUrl: 'ABC.Admin/CourseAdmin/GetLookupItems?lookupType=locations',
buildSelect: createSelectList
},
editrules: {
custom: true,
custom_func: locationCheck
}
},
{ name: 'Time.Id', index: 'Time.Id', width: 25, editable: false, hidden: true },
{ name: 'Time.Name', index: 'Time.Name', width: 50, editable: true, edittype: 'select',
editoptions: {
dataUrl: 'ABC.Admin/CourseAdmin/GetLookupItems?lookupType=times',
buildSelect: createSelectList
}
},
{ name: 'Cost.Id', index: 'Cost.Id', width: 25, editable: false, hidden: true },
{ name: 'Cost.Name', index: 'Cost.Name', width: 50, editable: true, edittype: 'select',
editoptions: {
dataUrl: 'ABC.Admin/CourseAdmin/GetLookupItems?lookupType=costs',
buildSelect: createSelectList
}
},
{ name: 'Instructor.Id', index: 'Instructor.Id', width: 25, editable: false, hidden: true },
{ name: 'Instructor.Name', index: 'Instructor.Name', width: 100, editable: true, edittype: 'select',
editoptions: {
dataUrl: 'ABC.Admin/CourseAdmin/GetLookupItems?lookupType=instructors',
buildSelect: createSelectList
}
},
{ name: 'AdditionalInfo', index: 'AdditionalInfo', width: 200, editable: true, edittype: 'textarea',
editoptions: {
rows: "6",
cols: "40"
}
},
],
pager: jQuery('#pager'),
rowNum: 20,
sortname: 'Date',
sortorder: "asc",
viewrecords: true,
caption: 'Scheduled Courses',
height: 400,
loadonce: true, // needs to be true for client side paging to work
autowidth: true,
loadtext: 'Loading...'
})
$("#grid").jqGrid('navGrid', '#pager', { edit: true, add: true, del: true },
{ /* edit options */
url: 'ABC.Admin/CourseAdmin/UpdateScheduledCourse/',
closeOnEscape: true,
closeAfterEdit: true,
width: 450
},
{ /* add options */
url: 'ABC.Admin/CourseAdmin/CreateScheduledCourse/',
closeOnEscape: true,
closeAfterAdd: true
},
{ /* delete options */
url: 'ABC.Admin/CourseAdmin/DeleteScheduledCourse/',
closeOnEscape: true
}
);
});
function locationCheck(value, colname) {
debugger;
if (value < 0 || value > 20)
return [false, "Please enter value between 0 and 20"];
else
return [true, ""];
}
控制器代码:
//colNames: ['Id', 'Date', 'LocationId', 'Location', 'TimeId', 'Time', 'CostId', 'Cost', 'InstructorId', 'Instructor', 'Additional Info'],
[HttpPost]
public ActionResult UpdateScheduledCourse(string oper, int id, string scheduledCourseDate, string location, string locationId, string timeId,
string timeName, string costId, string costName, string instructorId, string instructorName, string additionalInfo)
{
Models.ScheduledCourseProvider p = new Models.ScheduledCourseProvider();
//Models.ScheduledCourse o = new Models.ScheduledCourse(
// id, scheduledCourseDate, Convert.ToInt32(locationId), locationName, Convert.ToInt32(timeId), timeName, Convert.ToInt32(costId),
// costName, Convert.ToInt32(instructorId), instructorName, additionalInfo);
//bool saved = p.UpdateScheduledCourse(o);
bool saved = false;
if (!saved)
{
Response.StatusCode = 500;
return Content("Record not saved!");
}
else
{
return Json(false);
}
}
以下是在控制器中调用ActionResult方法时传递给服务器的参数(http://localhost:30320/OrchardLocal/ABC.Admin/CourseAdmin/UpdateScheduledCourse/)-注意所使用的点符号,因为我使用的是共享类型的LookupItem:
ScheduledCourseDate=07%2F01%2F2011&Location.Name=10016&Time.Name=1014&Cost.Name=1001&Instructor.Name=10001&AdditionalInfo=blahblahblah&oper=edit&id=12126
首先,您应该更改列的
name
属性,使其内部没有点。例如,您可以改为使用下划线。此外,我不确定您是否需要'Location.Id'
,'Time.Id'
和'讲师.Id'
。如果服务器上需要,可以使用name:'location',jsonmap:'location.name'
。同样,您可以使用name:'scheduledCourseDate',jsonmap:'scheduledCourseDate'
代替name:'scheduledCourseDate'
更改后,将通过短名称“位置”、“时间”和“讲师”发布所选ID。因此,您可以使用UpdateScheduledCourse
方法中的名称
[HttpPost]
public ActionResult UpdateScheduledCourse(字符串oper、int id、字符串scheduledCourseDate、,
字符串位置、字符串时间、字符串成本、字符串讲师、字符串附加信息)
在这方面取得了一些进展。。。看起来我可以在ActionResult方法中使用FormCollection类型。太棒了!我不知道jsonmap属性。我确实需要服务器b/c上的点符号,这些是强类型对象。Id和名称是位置、成本、时间和其他查找项的属性。索引属性也需要与jsonmap匹配吗?name:'location',jsonmap:'location.name',index:'location'@Tone:您需要将索引
指定为要作为sidx
参数发送到服务器的名称(发送到'ABC.Admin/CourseAdmin/GetScheduledCourses/“
)。当前您使用的是sortname:“日期”
。所以你有了sidx=Date
。如果用户单击'Location'
列的标题,新请求将与sidx=Location.Name一起发送。如果要使用name:'location'
而不使用任何索引
,则排序过程中将使用值sidx=location
。如果您想拥有sidx=blablabla.location
,那么应该包括索引:“blabla.location”
。属性索引
将仅用于sidx
。
ScheduledCourseDate=07%2F01%2F2011&Location.Name=10016&Time.Name=1014&Cost.Name=1001&Instructor.Name=10001&AdditionalInfo=blahblahblah&oper=edit&id=12126