簡體   English   中英

無法將其綁定到我的網格到Telerik OpenAccess數據源

[英]Unable to bind it to my Grid to a Telerik OpenAccess DataSource

我正在使用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得到的錯誤消息

  • 無法將屬性或索引器“ CustomProperty”分配給它-只讀

  • 在CustomProperty上定義“ set {}”后,此錯誤消息已解決,但我開始獲取其他錯誤消息

  • (...)如果'CustomProperty'是屬性,請向其添加FieldAlias或Storage屬性或將其聲明為字段的別名。

不要在視圖中使用您的OpenAccess對象。

嘗試這樣的事情(我還沒有測試過,只是根據內存來做):

ViewModel:

定義一個新類,其中包含視圖所需的所有數據。

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)
     )
)

通常,@ 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();
}

關鍵是客戶變量將沒有自己的CustomProperty。

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM