开发者

MVC: Replace A ID Value (of a Foreign Key) In A View By Its Value

Assume we have these two tables, binded by foreign key CAT_ID:

Category[CAT_ID,CAT_NAME]

cat_id = 1, cat_name= Classical

cat_id = 2, cat_name = Jazz

...

MusicAlbums[ALBUM_ID, ALBUM_NAME, CAT_ID]

album_id = 1, album_nam开发者_如何学JAVAe = Wagner - Parsifal, cat_id=1

album_id = 2, album_name = "Davis Miles - Kind of Blue, cat_id = 2

How can I display on MVC View instead of CAT_ID its value(that is : CAT_NAME) ?

like this:

---------

Music

1 | Wagner - Parsifal | Classical

2 | Davis Miles - Kind of Blue | Jazz

---------

Thank you


You want the Foreign Key feature (Its marked as new so may not have been available when you first looked):

http://demos.telerik.com/aspnet-mvc/grid/foreignkeycolumn

(View code (from their site, slightly cleaned up))

 Html.Telerik().Grid<ClientEditableOrder>()
       .Name("Grid")                      
       .Columns(columns =>
       {
           columns.Bound(o => o.OrderID).Width(100);
           columns.ForeignKey(o => o.EmployeeID, (IEnumerable<employee>)ViewData["employees"],
               "ID", "Name").Width(230);
           columns.Bound(o => o.OrderDate).Width(150);
           columns.Bound(o => o.Freight).Width(220);               
       })
       //..etc..
       .Render();

ViewData is set in the initial controller action:

public ActionResult Edit() {
        var r = _userService.GetAllemployees().ToList();
        ViewData["employees"] = r;
        return View();
    }

Their 'AjaxEdit' controller action does some funky stuff with explicitly listed foreign key ids in the parameters. But as my object exposed the FK fields I found in worked as expected without the need for any tricky business..

Bonus points to anyone who can provide an example of filtering/sorting on the displayed value on these columns as these features apply themselves to the underlying 'id' value rather than the displayed string (annoying!)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜