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

Help with Razor and Grid magic

4 Answers 21 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Anton Swanevelder
Top achievements
Rank 2
Anton Swanevelder asked on 13 Sep 2011, 11:14 AM
Hi,

I thought I will test the waters on the Razor MVC code but I definitely am a bit green on this front. Was hoping someone can just easily point me in the right direction. I basically have the code below which shows fine but none of the paging or any other ajax related things are working. I am sure I need to somehow tell it to post to _FirstLook but could not figure this out from the examples.

In my Index.cxhtml I have the following code:

@model IEnumerable<eSite.Calorie.Models.NutritionDto>
@(Html.Telerik().Grid(Model)
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(o => o.Description).Width(100);
        columns.Bound(o => o.Amount).Width(100);
        columns.Bound(o => o.Serving).Width(100);
        columns.Bound(o => o.Grams).Width(100);
        columns.Bound(o => o.Kcal_100).Width(100);
        columns.Bound(o => o.Kj_100).Width(100);
        columns.Bound(o => o.Kcal_serving).Width(100);
        columns.Bound(o => o.Kj_serving).Width(100);
    })
    .DataBinding(dataBinding =>
    {
        dataBinding.Ajax().Select("_FirstLook", "Grid").Enabled(true);
    })
    .Scrollable(scrolling => scrolling.Enabled(true))
        .Sortable(sorting => sorting.Enabled(true))
        .Pageable(paging => paging.Enabled(true))
        .Filterable(filtering => filtering.Enabled(true))
        .Groupable(grouping => grouping.Enabled(true))
        .Footer(true)
)

In the HomeController the following:

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Nutrition Search";
 
            return View(GetNutrition());
        }
 
        [GridAction]
        public ActionResult _FirstLook()
        {
            return View(new GridModel(GetNutrition()));
        }
 
        private IEnumerable<NutritionDto> GetNutrition()
        {
            NutritionEntities entity = new NutritionEntities();
 
            List<NutritionDto> list = new List<NutritionDto>(entity.EnergySearch("%soup%").Select(order => new NutritionDto
            {
                ID = order.NDB_No,
                Description = order.Description,
                Grams = order.g.ToString(),
                Amount = order.Amount.ToString(),
                Kcal_100 = order.kcal_100g.ToString(),
                Kj_100 = order.kj_100g.ToString(),
                Kcal_serving = order.kcal_serving.ToString(),
                Kj_serving = order.kj_serving.ToString(),
                Serving = order.Serving
            }));
 
            return list;
        }
    }

The NutritionDto

[KnownType(typeof(NutritionDto))]
    public class NutritionDto
    {
        public string ID { get; set; }
        public string Description { get; set; }
        public string Amount { get; set; }
        public string Serving { get; set; }
        public string Grams { get; set; }
        public string Kcal_100 { get; set; }
        public string Kj_100 { get; set; }
        public string Kcal_serving { get; set; }
        public string Kj_serving { get; set; }
    }

The back-end is ADO.Net EF

4 Answers, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 13 Sep 2011, 12:09 PM
Hi Anton Swanevelder,

Looking at the code you have pasted, it seems that controller name those not match to the one set in the grid's select route. 

Greetings,
Rosen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Anton Swanevelder
Top achievements
Rank 2
answered on 13 Sep 2011, 02:15 PM
Hi Rosen,

Can you supply a few more dots here. My controller's name is HomeController in your example you are using GridController yet the markup only refers to "Grid" so where is "Grid" defined?

If I change the code as follows is has no effect, I guess it needs to be instantiated somewhere.

.DataBinding(dataBinding =>
    {
        dataBinding.Ajax().Select("_FirstLook", "HomeController").Enabled(true);
    })


0
Accepted
Rosen
Telerik team
answered on 13 Sep 2011, 02:38 PM
Hello Anton Swanevelder,

In ASP.NET MVC framework all controller names should end with Controller suffix. Therefore, when used in a route GridController should be refer to as Grid (i.e. HomeController -> Home). More information on MVC framework can be found here under Learning Resources.

Regards,
Rosen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Anton Swanevelder
Top achievements
Rank 2
answered on 13 Sep 2011, 02:41 PM
Thank you, got quite a bit to learn to catch-up with the Razor MVC world :)

Your documentation on the GridBindingSettingsBuilder also show the correct use to be "Home".
http://www.telerik.com/help/aspnet-mvc/m_telerik_web_mvc_ui_fluent_gridbindingsettingsbuilder_select_3.html
Tags
Grid
Asked by
Anton Swanevelder
Top achievements
Rank 2
Answers by
Rosen
Telerik team
Anton Swanevelder
Top achievements
Rank 2
Share this question
or