Pass a hidden jqGrid value when editing on ASP.Net MVC
I have a jqGrid in an ASP.Net MVC.
The grid is defined as:
$("#list").jqGrid({
url: '<%= Url.Action("History", "Farrier", new { id = ViewData["horseId"]}) %>',
editurl: '/Farrier/Add',
datatype: 'json',
mtype: 'GET',
colNames: ['horseId', 'date', 'notes'],
colModel: [
{ name: 'horseId', index: 'horseId', width: 250, align: 'left', editable:false, editrules: {edithidden: true}, hidden: true },
{ name: 'date', index: 'farrierDate', width: 250, align: 'left', editable:true },
{ name: 'notes', index: 'farrierNotes', width: 100, align: 'left', editable: true }
],
pager: jQuery('#pager'),
rowNum: 5,
rowList: [5, 10, 20, 50],
sortname: 'farrierDate',
sortorder: "DESC",
viewrecords: true
});
What I want to be able to do, add a row to the grid, where the horseId is either a) not displayed or b) greyed out. But is passed to the controller when saving.
The way it's set up is this grid will only have 1 horse id at a time (it will exist on a horse's property page.)
The only time I've gotten anything to work is when I made it editable, but then that opens it up for the user to modify the id, which isn't a good idea.
So is there some way I can set this value before submitting the data? it does exist as a variable on this page, if that helps any (an开发者_如何学God I've checked that it isn't null).
Thanks
I ended up defining a jscript variable with a ViewData value. Then used that for the editoption: defaultvalue property.
Here's my script:
var defVal = '<%= ViewData["horseId"] %>';
$(function() {
$("#list").jqGrid({
url: '<%= Url.Action("History", "Farrier", new { id = ViewData["horseId"]}) %>',
editurl: '/Farrier/Add',
datatype: 'json',
mtype: 'GET',
colNames: ['horseId', 'date', 'notes'],
colModel: [
{ name: 'horseId', index: 'horseId', width: 250, align: 'left', editable: true, editoptions: { defaultValue:defVal}, hidden:true },
{ name: 'date', index: 'farrierDate', width: 100, align: 'left', formatter:'date', datefmt: 'mm/dd/yyyy', editable: true, editrules: {date:true} },
{ name: 'notes', index: 'farrierNotes', width: 500, align: 'left', editable: true, edittype: 'textarea' }
],
pager: jQuery('#pager'),
rowNum: 5,
rowList: [5, 10, 20, 50],
sortname: 'farrierDate',
sortorder: "DESC",
viewrecords: true,
width: 850
});
jQuery("#list").navGrid('#pager', { add: true });
});
精彩评论