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

[Solved] Trouble with dropdownlists and EntityFramwork

2 Answers 143 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.
Michael Pullella
Top achievements
Rank 1
Michael Pullella asked on 13 Jan 2012, 08:23 PM
Hi

I am having trouble getting the grid to display a dropdown list when using the EntityFramework as the datasource.

My EntityFramwork exposes a property for Customer.
My Controller is getting the list of Customers and binding it to the grid.
The Customer has a property called "Industry"

When I edit the grid I want the list of Industries to appear as a dropdown list in the grid.

EditorTemplate looks like the following:
@model string
  
@(
    Html.Telerik().DropDownListFor(e => e).BindTo(new SelectList(ViewBag.Industries))
)

When I edit the customer row, the Industry field appears as a textbox.

I read that the UIHint should be applied to the Industry property, but b/c the EntityFramework genereates the Customer class, how do I apply the UIHInt attribute to the Industry property?

2 Answers, 1 is accepted

Sort by
0
Accepted
John DeVight
Top achievements
Rank 1
answered on 13 Jan 2012, 08:33 PM
Hi Michael,

I ran into a similar issue.  Since the model classes that are generated by the EntityFramework are parital classes, I was able to create a new property for the model class that contained the UIHint attribute and returned the value of the property that I wanted to display as a dropdownlist.

So, in your example, instead of trying to display the Industry property as a dropdownlist, try the following:

Create a new class file.  In the class file, create a class that looks similar to the following:

namespace MyApplication
{
    public partial class Customer
    {
        [UIHint("IndustryDropdown")]
        public string IndustryDropdown
        {
            get { return this.Industry; }
            set { this.Industry = value; }
        }
    }
}

In your grid, instead of binding the column to the Industry property, bind it to the IndustryDropdown property:

@{
    Html.Telerik().Grid(Model)
        .Name("CustomerGrid")
        .DataKeys(keys => keys.Add(m => m.Id))
        .Columns(columns =>
        {
            columns.Bound(m => m.Name);
            columns.Bound(m => m.IndustryDropdown).Title("Industry");
            columns.Command(cmd => cmd.Edit());
        })
        .DataBinding(dataBinding => dataBinding.Ajax().Update("UpdatePerson", "Home"))
        .Render();
}

Finally, rename your EditorTemplate to: IndustryDropdown.cshtml

Hope this helps.

Regards,

John DeVight
0
Michael Pullella
Top achievements
Rank 1
answered on 13 Jan 2012, 08:35 PM
Thanks so much!  This works perfectly.
Tags
Grid
Asked by
Michael Pullella
Top achievements
Rank 1
Answers by
John DeVight
Top achievements
Rank 1
Michael Pullella
Top achievements
Rank 1
Share this question
or