This is a migrated thread and some comments may be shown as answers.

Binding Non-persistent Property

4 Answers 123 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Rubens
Top achievements
Rank 1
Rubens asked on 29 Sep 2014, 06:37 PM
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.

4 Answers, 1 is accepted

Sort by
0
Doroteya
Telerik team
answered on 02 Oct 2014, 02:46 PM
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.
 
0
Rubens
Top achievements
Rank 1
answered on 03 Oct 2014, 05:40 AM
Dear Doroteya,

I really appreciate 
0
Rubens
Top achievements
Rank 1
answered on 03 Oct 2014, 05:52 AM
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 ;) 

0
Doroteya
Telerik team
answered on 08 Oct 2014, 05:18 AM
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.
 
Tags
Data Access Free Edition
Asked by
Rubens
Top achievements
Rank 1
Answers by
Doroteya
Telerik team
Rubens
Top achievements
Rank 1
Share this question
or