我正在尝试使用MVC实现jqGrid。
模型是:
public class InvoiceHeader
{
public int id { get; set; }
public DateTime invdate { get; set; }
public int clientid { get; set; }
public decimal amount { get; set; }
[Display(Name = "Invoice Number:")]
public int tax { get; set; }
public decimal total { get; set; }
[Required(ErrorMessage = "Note is a required field.")]
[Display(Name = "Note:")]
public string note { get; set; }
public int PaymentTypeId { get; set; }
public string CreatedByUsername { get; set; }
public virtual PaymentType PaymentType { get; set; }
}
我的观点是:
@model JQGRID.Models.InvoiceHeader
@{
ViewBag.Title = "Home Page";
}
@*<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />*@ <title>My First
Grid</title>@*<link rel="stylesheet" type="text/css" media="screen" href="css/ui-lightness/jquery-ui-1.7.1.custom.css" />*@
<link rel="stylesheet" type="text/css" media="screen" href="../../Scripts/css/ui.jqgrid.css" />
<link rel="stylesheet" type="text/css" media="screen" href="../../Scripts/css/ui.multiselect.css" />
@*<link rel="stylesheet" type="text/css" media="screen" href="../../Content/themes/ui-lightness/jquery-ui-1.8.14.custom.css" />*@
<style>
html, body
{
margin: 0;
padding: 0;
font-size: 75%;
}
</style>
<script src="../../Scripts/js/jquery-1.5.2.min.js" type="text/javascript"></script>
<script src="../../Scripts/js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="../../Scripts/js/jquery.jqGrid.min.js" type="text/javascript"></script>
@*<script src="../../Scripts/src/grid.inlinedit.js" type="text/javascript"></script>*@
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery("#list").jqGrid({
url: '/Home/DynamicGridData/',
datatype: 'json',
mtype: 'POST',
colNames: ['id', 'note', 'tax', 'PaymentType', 'CreatedByUsername', 'Actions'],
colModel: [
{ name: 'id', index: 'id', hidden: true, editable: false },
{ name: 'note', index: 'note', width: 40,align:'center', editable: true, editrules: { required : true } },
{ name: 'tax', index: 'tax', width: 400, align: 'center', editable: true, editrules: { required : true } },
{ name: 'PaymentTypeId', index: 'PaymentTypeId', width: 400, align: 'center', editable: true, edittype:"select",
editoptions: { dataUrl: '/Home/PaymentTypes/' }},
{ name: 'CreatedByUsername', index: 'CreatedByUsername', hidden: true, editable: false },
{ name: 'act',index:'act',width:55,align:'center',sortable:false,formatter:'actions',
formatoptions:{
keys: true, // we want use [Enter] key to save the row and [Esc] to cancel editing.
beforeSubmit:function(postdata, formid) {
alert("Hi");
jQuery("#ValidationSummary").show('slow');
},
},}
],
pager: jQuery('#pager'),
rowNum: 10,
rowList: [5, 10, 20, 50],
sortname: 'Id',
sortorder: "desc",
viewrecords: true,
imgpath: '',
caption: 'My first grid',
editurl: '/Home/Save/'
});
jQuery("#list").navGrid('#pager', { edit: false, search: false, add: true });
});
</script>
@using (Html.BeginForm()) {
@Html.ValidationSummary()
<table id="list">
</table>
<div id="pager">
</div>}
正如您在Save中看到的那样,在控制器中调用以下方法:
public void Save(InvoiceHeader invoiceHeader)
{
if (ModelState.IsValid) {
...
try {
context.SaveChanges();
}
catch (Exception ex) {
}
}
}
我不明白为什么当您输入此方法并检查invoiceHeader
属性时,会填充id属性但未填充CreatedByUsername
属性。
我真的需要填充这个属性才能保存。
有人可以向我解释为什么会这样,以及如何填充CreatedByUsername
?
答案 0 :(得分:0)
您是否在“保存”操作中检查了Id对象的值?它们应该是网格中的行ID,而不是您正在编辑的对象的实际ID。
为防止出现这种情况,您应尝试为Id
和CreatedByUserName
列添加列,并使其不可见。另外,在您的Id列中添加key:true
值。这样,在编辑时,将从这些列填充值。