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

后端 未结 3 715
太阳男子
太阳男子 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条回答
  • 2021-01-07 00:13

    Currently I'm facing same problem as you write and it is really true what Telerik guys wrote you. Partial view is pre-rendered on server (content included). This may be sufficient solution, if the list of allowed values is static, but...

    ...imagine that you want to have different list of allowed values for each grid row. In that case this concept is not feasible...

    Since there is just one combo (per column) within grid, only one solution I found is to handle onEdit grid event where you can databind combo box to allowed values using AJAX. In grid onEdit handler you can access all data fields of proper row, so you can use them for binding purposes.

    Regards, Ondrej.

    0 讨论(0)
  • 2021-01-07 00:14

    I asked the good support folks at Telerik about this. Here is the answer they gave me:

    The Model is null because the DropDownList partial view is rendered for ajax editing. In that case the grid prerenders all partial view editor templates so it can use them on the client-side. In that case Model will be null. If you used server binding and editing the Model would be set to the right value.

    At this point, I'm going to accept this post as the answer to my question. It's unfortunate that I have to accept my own answer in this case, but ... well, I didn't get any others to choose from :)

    0 讨论(0)
  • 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<ProductPresentationModel>
                        {
                            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?

    0 讨论(0)
提交回复
热议问题