Telerik mvc grid, Model Field to db field name relation
I have a Telerik mvc grid. I am trying to get sorting to work. I am binding my grid to a model collection. The model has different names for fields than the data layer.
When i click a sort button the grid passes the sort column name as the property name for the model that i am binding. I want to set that to be a different name, as to have it match my data access layer. I have done this previously by adding an attribute to the properties of the model that specify the related database field names. Is there anything similar in Telerik's grid? I have tried [Column(Name = "Myname")]but that seems to be ignored.
I do not want to configure the grid in a开发者_开发问答ny way to know these names. The UI layer should not be aware of the DB layer's field names. Is there any way i can set this up within my model?I am not sure if I understand what you are trying to do. Example code would probably help. That being said I will try to answer your question. You could override the name in the method on the controller that you are using to sort the Telerik grid. I have this same situation and I don't have a problem with the sorting. I am binding the grid directly to the Model and it doesn't access the data access logic at all. The telerik grid uses Linq so it should just do the sorting on your collection and not have to reconnect to the data access logic. I have included an example of the controller method that I am calling when doing sorting on my Telerik grid. Orderbll.GetAll returns a collection of orders.
[GridAction]
public ActionResult AjaxIndex()
{
// Get all of the orders
return View(new GridModel(orderBll.GetAll()));
}
As I understand it, your model is what you're querying, and should be consistent so that you can query the database. If you want different text in the headings of your grid columns, you can do that with the Title()
method when binding the column:
.Columns(columns =>
{
columns.Bound(a => a.Surname).Title("Family name");
....
精彩评论