我正在使用asp.net mvc项目,我正在使用填充了模型的网格。但是,我尝试在网格中创建新记录时遇到错误,说Uncaught ReferenceError: CustomerContract is not defined
。
这是我的网格和模板:
<script type="text/kendo" id="customerTemplate">
#if(data != null){#
#:data.Name#
#}#
</script>
<script type="text/javascript">
var customerTemplate = kendo.template($("#customerTemplate").html(), { useWithBlock: false });
</script>
@(Html.Kendo().Grid<ProjectModel>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(p => p.Name);
columns.Bound(p => p.CustomerContract).ClientTemplate("#:customerTemplate(CustomerContract)#");
columns.Command(command =>
{
command.Edit();
command.Destroy();
}).Width(180);
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("ProjectPopUpTemplate"))
.Pageable()
.Sortable()
.Scrollable()
.HtmlAttributes(new {style = "height:500px;"})
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Events(events => events.Error("error_handler"))
.Model(model => { model.Id(p => p.Id); })
.Create(update => update.Action("EditingPopup_Create", "ProjectManagement"))
.Read(read => read.Action("EditingPopup_Read", "ProjectManagement"))
.Update(update => update.Action("EditingPopup_Update", "ProjectManagement"))
.Destroy(destroy => destroy.Action("EditingPopup_Destroy", "ProjectManagement"))
)
)
如果我选择不在CustomerContract列上使用模板(单元格值只是[object Object]),则单击&#34;添加新记录&#34;弹出窗口正在工作。网格中的按钮。
这是我的阅读方法:
public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request)
{
ProjectContract[] list = ProjectService.GetProjects();
return Json(list.ToDataSourceResult(request));
}
ProjectContract包含CustomerContract作为属性。
型号:
public class ProjectModel
{
private ProjectContract _projectContract;
public ProjectModel(ProjectContract projectContract)
{
ProjectContract = projectContract;
}
public ProjectModel()
{
}
public CustomerServiceClient CustomerService { get; set; }
private ProjectContract ProjectContract
{
get
{
if (_projectContract == null)
{
_projectContract = new ProjectContract();
}
return _projectContract;
}
set { _projectContract = value; }
}
[Display(Name = "Customer")]
public CustomerContract CustomerContract
{
get
{
if (ProjectContract.CustomerContract == null)
{
return new CustomerContract();
}
return ProjectContract.CustomerContract;
}
set { ProjectContract.CustomerContract = value; }
}
[Display(Name = "Customers")]
public List<Customer> Customers { get; set; }
[ScaffoldColumn(false)]
public int Id
{
get { return ProjectContract.Id; }
set { ProjectContract.Id = value; }
}
[Display(Name = "Project Name")]
public string Name
{
get { return ProjectContract.Name; }
set { ProjectContract.Name = value; }
}
}
我希望有人可以指出我正确的方向,这里发生了什么,以及为什么我无法添加新记录。
答案 0 :(得分:4)
在网格的模型部分。为新客户合同添加一个字段,并初始化一个新的客户合同,如下所示:
Model(model => {
model.Id(p => p.id);
model.Field(p => p.CustomerContract).default(new CustomerContract());
}
这应该解决你的问题。
如果你需要一个更全面的例子我可以在我的电脑上做一个。