无法将其绑定到我的网格到Telerik OpenAccess数据源

时间:2014-09-29 16:27:01

标签: c# .net kendo-ui kendo-grid kendo-asp.net-mvc

我使用ASP.MVC,Kendo和OpenAccess开发应用程序。

在为特定实体创建自定义属性后,我尝试将其绑定到我的数据源和网格失败。

部分班级

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ErpMvc.OpenAccess
{
    public partial class Customer
    {
        public string CustomProperty
        {
            get
            {
                return "My Custom Property Text";
            }
        }
    }
}

服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ErpMvc.OpenAccess;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;

namespace ErpMvc.Services
{
    public class CustomerService
    {
        public static IEnumerable<Customer> GetCustomers()
        {
            var dbContext = new EntitiesModel();

            return dbContext.Customers.Select(customer => new Customer
            {
                CustomerID = customer.CustomerID,
                FirstName = customer.FirstName,
                CustomProperty = customer.CustomProperty
            });

        }
    }
}

查看

@model IEnumerable<ErpMvc.OpenAccess.Customer>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@(Html.Kendo().Grid(Model)
    .Name("Customers")
    .Columns(columns =>
    {
        columns.Bound(c => c.FirstName).Title("First Name");
        columns.Bound(c => c.CustomProperty).Title("Custom Property");
    })
    .Pageable()
    .Sortable()
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .DataSource(dataSource => dataSource
        .Ajax()
        .Model(model => model.Id(customerID => customerID.CustomerID))
        .Read(read => read.Action("Customers_Read", "Customer"))
        .Update(update => update.Action("Customers_Update", "Customer"))
        .PageSize(50)
     )
)

控制器

public ActionResult Customers_Read([DataSourceRequest] DataSourceRequest request)
{
    return Json(CustomerService.GetCustomers().ToDataSourceResult(request));
}
  • 我从VS

  • 获取的错误消息
  • 属性或索引器&#39; CustomProperty&#39;无法分配 - 它是只读的

  • 在定义了&#34; set {}&#34;在我的CustomProperty上,此错误消息已解决,但我开始使用另一个

  • (...)如果&#39; CustomProperty&#39;属性是请将FieldAlias或Storage属性添加到其中,或将其声明为字段别名。

2 个答案:

答案 0 :(得分:0)

请勿在视图中使用您的OpenAccess对象。

尝试这样的事情(我没有测试过这个,只是通过记忆):

视图模型:

定义一个新类,其中包含视图所需的所有数据。

namespace ErpMvc.ViewModel
{
    public class Customer
    {
        public int CustomerID { get; set; }
        public string FirstName { get; set; }
        public string CustomProperty { get; set; }
    }
}

服务:

服务需要使用OpenAccess检索数据,然后将此数据传输到视图模型。

public static IQueryable<Customer> GetCustomers()
{
    IQueryable<Customer> result = null;
    using(var dbContext = new EntitiesModel()) 
    {
        result = dbContext.Customers.Select(customer => new ViewModel.Customer
        {
            CustomerID = customer.CustomerID,
            FirstName = customer.FirstName,
            CustomProperty = customer.CustomProperty
        });
        return result;        
    }
}

控制器:

public ActionResult Customers_Read([DataSourceRequest] DataSourceRequest request)
{
    return Json(CustomerService.GetCustomers().ToDataSourceResult(request));
}

查看:

模型未传递到网格构造函数中,因为您正在使用Read()。这也意味着您不需要在视图顶部定义模型。

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@(Html.Kendo().Grid<ErpMvc.ViewModel.Customer>()
    .Name("Customers")
    .Columns(columns =>
    {
        columns.Bound(c => c.FirstName).Title("First Name");
        columns.Bound(c => c.CustomProperty).Title("Custom Property");
    })
    .Pageable()
    .Sortable()
    .Editable(editable => editable.Mode(GridEditMode.InLine))
    .DataSource(dataSource => dataSource
        .Ajax()
        .Model(model => model.Id(customerID => customerID.CustomerID))
        .Read(read => read.Action("Customers_Read", "Customer"))
        .Update(update => update.Action("Customers_Update", "Customer"))
        .PageSize(50)
     )
)

答案 1 :(得分:0)

一般来说,@ Nic建议的方法是正确的。从OpenAccess的角度来看,您需要进一步修改端点中的查询。它应该类似于以下内容:

public static IEnumerable<ViewModel.Customer> GetCustomers()
{
    var dbContext = new EntitiesModel();

    return dbContext.Customers.Select(customer => new ViewModel.Customer
    {
        CustomerID = customer.CustomerID,
        FirstName = customer.FirstName,
    }).ToList();
}

关键是 customer 变量没有自己的CustomProperty。

我希望这会有所帮助。