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

[Solved] Grid edit with dropdown - I yield!!

4 Answers 294 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.
Martin Moesby
Top achievements
Rank 2
Martin Moesby asked on 13 Jan 2012, 01:30 PM
OK, I have officially given up....

I am desperately trying to display a dropdown when adding/editing and item in gridview....
I don't know if my situation is special, but so fat, I have read most of what I could find about grid and dropdown.
Background:
  • I created a MVC3 project with EntityFramework 4.1, Database first.
  • I added my tables to the EDMX designer.
  • I created my poco-objects using the ADO.NET DbContext creator.
  • Altered properties in the entity, for which I want a dropdown when creating/editing...
  • Created a DropDown.cshtml i the Shared/EditorTemplates
  • I created a controller using scaffolding for List, Edit, Create and Delete
  • In the create/edit-action of my controller, I created the ViewData-object with my data for the dropdown.

SO far so good... I think I have remembered all the steps.....
OK, heres my code :

The Entity

    public partial class Product
    {
        public Product()
        {
            this.CustomerMargins = new HashSet<CustomerMargin>();
            this.Prices = new HashSet<Price>();
        }
     
        public int Id { get; set; }
        public string ProductNumber { get; set; }
        public string ProductName { get; set; }
        public string CommonProductNumber { get; set; }
 
        [UIHint("DropDown")]
        public int ProductGroup_Id { get; set; }
 
        [UIHint("DropDown")]
        public int ProductMainGroup_Id { get; set; }
     
        public virtual ICollection<CustomerMargin> CustomerMargins { get; set; }
        public virtual ICollection<Price> Prices { get; set; }
 
        public virtual ProductGroup ProductGroup { get; set; }
 
        public virtual ProductMainGroup ProductMainGroup { get; set; }
    }
}

The virtual properties er navigational properties...

The View

@model IEnumerable<ProductPriceManagement.Models.Product>
 
@{
    ViewBag.Title = "Product";
}
<h2>
    Product Management</h2>
@(Html.Telerik().Grid(Model)
    .Name("Grid")
    .DataKeys(keys => keys.Add(o => o.Id))
    .DataBinding(dataBinding => dataBinding
        .Server()
            .Select("Index", "Product")
            .Update("Edit", "Product")
            )
    .Columns(columns =>
    {
        columns.Bound(o => o.CommonProductNumber).Title("Nordic nr").Width(100);
        columns.Bound(o => o.ProductMainGroup.Description).Title("Main Group").Width(200);
        columns.Bound(o => o.ProductGroup.Description).Title("Product Group").Width(200);
        columns.Bound(o => o.ProductNumber).Width(120);
        columns.Bound(o => o.ProductName).Width(250);
        columns.Command(commands => commands.Edit().ButtonType(GridButtonType.BareImage)).Width(100);
    })
    .Editable(editing => editing.Mode(GridEditMode.InLine))
    .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)
)
<p>
    @Html.ActionLink("Create new", "Create", "Product")
</p>

The DropDown.cshtml

model ProductPriceManagement.Models.Product
@(Html.Telerik().DropDownList()       
    .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
        .BindTo(new SelectList((System.Collections.IEnumerable)ViewData["ProductGroup_Id"], "Id", "Name", Model.ProductGroup_Id))
        )

The Controller

// GET: /Product/Create
public ActionResult Create()
{
PopulateGroupLists();
return View();
}
//
// POST: /Product/Create
[HttpPost]
public ActionResult Create(Product product)
{
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
PopulateGroupLists();
return View(product);
}
protected void PopulateGroupLists()
{
ViewData["ProductGroup_Id"] = new SelectList(db.ProductGroups, "Id", "Description");
ViewData["ProductMainGroup_Id"] = new SelectList(db.ProductMainGroups, "Id", "Description");
ViewBag.ProductGroup_Id = new SelectList(db.ProductGroups, "Id", "Description");
ViewBag.ProductMainGroup_Id = new SelectList(db.ProductMainGroups, "Id", "Description");
}
(Some code in the controller has been omitted)

One more note - the dropdown.cshtml is very specific in the data, so dont mind the MainGroup-dropdown for now (even though I would like to understand, how I can reuse the Template with a different ViewData-object)

So there it is.....
and I have oficially given up.... The Dropdown doesn't show, neither in create or edit...
I've spend quite a long tme on this I i have come to the conclusion, that I need professional help - so anyone want to catch this?

Regards,
Martin Moesby

4 Answers, 1 is accepted

Sort by
0
Bob
Top achievements
Rank 1
answered on 13 Jan 2012, 05:29 PM
I'm using ForeignKey columns. http://demos.telerik.com/aspnet-mvc/grid/foreignkeycolumn 

Have you tried that?

BOb
0
Accepted
Bob
Top achievements
Rank 1
answered on 13 Jan 2012, 05:32 PM
Not sure what's up with that last post. 

I'm using foreignkeycolumns to do this: http://demos.telerik.com/aspnet-mvc/grid/foreignkeycolumn 

It works really well. Just make sure you have the default "GridForeignKey.cshtml" editor tempate in your Views/Shared/EditorTemplates folder.

BOb

0
Martin Moesby
Top achievements
Rank 2
answered on 14 Jan 2012, 11:53 AM
Thank you !!!!!!

A really simple solution, that works just as expected !!!
You really saved my butt! :)

/Martin Moesby
0
Martin Moesby
Top achievements
Rank 2
answered on 14 Jan 2012, 03:28 PM
Just one tiny remark - the dropdown doesn't appear when using PopUp or InForm as the editmode. Only when using InLine as Edit mode does the Dropdown appear....


But that's good enough for now...

Thx again.
/MartinMoesby
Tags
Grid
Asked by
Martin Moesby
Top achievements
Rank 2
Answers by
Bob
Top achievements
Rank 1
Martin Moesby
Top achievements
Rank 2
Share this question
or