Binding Non-persistent Property

5 posts, 0 answers
  1. Rubens
    Rubens avatar
    4 posts
    Member since:
    Oct 2012

    Posted 29 Sep 2014 Link to this post

    Hi,

    Im developing an application using ASP.MVC, Kendo and OpenAccess.

    After created a custom property for an specific entity, I'm trying unsuccessfully to bind it to my Datasource and Grid.

    Partial Class
    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";
                }
            }
        }
    }


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

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


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


    Error messages I'm getting from VS

    • Property or indexer 'CustomProperty' cannot be assigned to -- it is read only

    After defined a "set{}" on my CustomProperty, this error messages was resolved but I started get this other one

    • (...) If 'CustomProperty' is a property please add the FieldAlias or Storage attribute to it or declare it as a field's alias.
  2. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 02 Oct 2014 Link to this post

    Hello Rubens,

    From Data Access' perspective, in the LINQ queries, you cannot use a non-persistent property (a property, which is not mapped to column in the database). To implement the scenario on your side, you can add a DTO class in your MVC application, which will be used to hold the query result. The DTO can have all the persistent properties you need, and the non-persistent property as well. The query also has to be changed a little bit, in order to consume the DTO. Finally the grid has to be configured to display objects of the DTO type. For example:

    The DTO class
    public class CustomerDTO
    {
        public int CustomerID { get; set; }
        public string FirstName { get; set; }
        public string CustomProperty
        {
            get
            {
                return "My Custom Property Text";
            }
        }
    }

    The query:
    public static IEnumerable<CustomerDTO> GetCustomers()
    {
        var dbContext = new EntitiesModel();
     
        return dbContext.Customers.Select(customer => new CustomerDTO
        {
            CustomerID = customer.CustomerID,
            FirstName = customer.FirstName,
        }).ToList();
    }

    The binding of the grid in the view is demonstrated in this documentation article.

    I hope this helps. If you have additional questions, do not hesitate to get back to us.


    Regards,
    Doroteya
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
  3. DevCraft banner
  4. Rubens
    Rubens avatar
    4 posts
    Member since:
    Oct 2012

    Posted 03 Oct 2014 in reply to Doroteya Link to this post

    Dear Doroteya,

    I really appreciate 
  5. Rubens
    Rubens avatar
    4 posts
    Member since:
    Oct 2012

    Posted 03 Oct 2014 in reply to Doroteya Link to this post

    Dear Doroteya,

    I really appreciate your help, perhaps I was not clear... but all that I want is nothing else than create a custom computed property to display on my grid that is an extension from a class. Something like "FullName" witch returns a combination of First and Last name.

    I wouldn't like to do that directly on my View... would be much better if I could make get this value by MyClass.CustomProperty

    I'm still a beginner... btw, should I really use a Data Transfer Object class for this purpose?

    Thank you ;) 

  6. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 08 Oct 2014 Link to this post

    Hi Rubens,

    You can achieve such an outcome by simply adding a field and for the property and a set clause in it in your initial code. This will prevent the error from appearing again.

    I hope this helps. If you need further information, do not hesitate to get back to us.


    Regards,
    Doroteya
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
Back to Top
DevCraft banner