admin管理员组

文章数量:1417664

I'm working on an asp mvc project where I'm using a grid populated with a model. However, I've encountered an error when trying to create a new record in my grid, saying Uncaught ReferenceError: CustomerContract is not defined.

Here's my grid and templates:

<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(mand =>
          {
              mand.Edit();
              mand.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"))
      )
)

If I choose to not use the template on the CustomerContract column (cell value will only be [object Object]), the popup window is working when clicking the "Add new record" button in the grid.

Here's my read method:

public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request)
{
    ProjectContract[] list = ProjectService.GetProjects();
    return Json(list.ToDataSourceResult(request));
}

Where the ProjectContract contains the CustomerContract as a property.

Model:

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; }
        }
    }

I was hoping someone could point me in the right direction as to what is going on here, and why I'm not able to add a new record.

I'm working on an asp mvc project where I'm using a grid populated with a model. However, I've encountered an error when trying to create a new record in my grid, saying Uncaught ReferenceError: CustomerContract is not defined.

Here's my grid and templates:

<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(mand =>
          {
              mand.Edit();
              mand.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"))
      )
)

If I choose to not use the template on the CustomerContract column (cell value will only be [object Object]), the popup window is working when clicking the "Add new record" button in the grid.

Here's my read method:

public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request)
{
    ProjectContract[] list = ProjectService.GetProjects();
    return Json(list.ToDataSourceResult(request));
}

Where the ProjectContract contains the CustomerContract as a property.

Model:

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; }
        }
    }

I was hoping someone could point me in the right direction as to what is going on here, and why I'm not able to add a new record.

Share Improve this question edited May 21, 2014 at 10:42 Nicklas Pouey-Winger asked May 21, 2014 at 8:48 Nicklas Pouey-WingerNicklas Pouey-Winger 3,0237 gold badges47 silver badges76 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

In the model section of the grid. Add a field for the new customer contract and initialise a new customer contract like this:

Model(model => {
    model.Id(p => p.id);
    model.Field(p => p.CustomerContract).default(new CustomerContract());
}

This should sort out your problems.

If you need a fuller example I can do one when I am on my PC.

本文标签: