Telerik MVC Grid: How to use DropDownList in a column?

后端 未结 3 790
太阳男子
太阳男子 2021-01-06 23:48

I have a Telerik MVC grid, in an MVC 3 application with Razor, which is being Ajax-bound. I am now trying to add a drop list column to it, so that users can use it in edit m

3条回答
  •  Happy的楠姐
    2021-01-07 00:19

    I managed to work this out, somewhat, but I still have a question. here's what I changed to get it working:

    Created a ViewData object in the controller, like this ...

    public ActionResult Index()
    {
        // ViewData object here ...
        ViewData["ProductCategories"] = new SelectList(_productCategoryService.GetActiveProductCategories(), "ProductCategoryID", "ProductCategoryName");
        var products = _productService.GetProducts().ToList();
        var presentationModel = _mapper.MapAsList(products);
        return View(presentationModel);
    }
    
    //
    // GET: /Product/
    [GridAction]
    public ViewResult _Index()
    {
        // ViewData object here ...
        ViewData["ProductCategories"] = new SelectList(_productCategoryService.GetActiveProductCategories(), "ProductCategoryID", "ProductCategoryName");
        return View(new GridModel
                        {
                            Data = _mapper.MapAsList(_productService.GetProducts().ToList())
                        });
    }
    

    Then, I used the ViewData object in the DropDownListHelper, like this ...

    @using System.Collections
    @model Models.PresentationModels.ProductPresentationModel
    
    @(Html.Telerik().DropDownList()
            .Name("ProductCategoryName")
            .BindTo(new SelectList((IEnumerable)ViewData["ProductCategories"], "Value", "Text"))
    );
    

    My question now is ... do I have to use the ViewData object? I'd love to be able to use a property off of my model. But, for some reason my Model is always NULL inside the Helper file. And, if I try to place the DropDownList code inside the Grid creation code, the DropDownList doesn't work at all.

    So, do I have any other options?

提交回复
热议问题