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

Batch Editing ForeignKey Column error

4 Answers 75 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.
Matthew
Top achievements
Rank 1
Matthew asked on 20 Nov 2011, 06:25 AM

Without the foreignkey column my Grid works flawlessly.  It's a beautiful thing.  However, with the foreignkey column I get the following
error:
----------------------------------------------------------------------------------------------------------------------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0305: Using the generic type 'System.Collections.Generic.IEnumerable<T>' requires 1 type arguments

Source Error:

 

Line 13:         c.Bound(p => p.PowderNum).Width(90).Title("Number");
Line 14:         c.Bound(p => p.PowderName).Width(210).Title("Description");
Line 15:         c.ForeignKey(p => p.ColorId, (IEnumerable)ViewData["htmlcolors"], "ColorId", "HtmlName");
----------------------------------------------------------------------------------------------------------------------------------------
I took apart the foreigh key column Demo and I can't find the source of the error in my code.... 
----------------------------------------------------------------------------------------------------------------------------------------
View (snipit)
----------------------------------------------------------------------------------------------------------------------------------------

@(Html.Telerik().Grid<Line.OpenAccess.Powders>()
    .Name("grid1")
    .DataKeys(k => k.Add(o => o.PowderId))
    .Columns(c =>{
        c.Bound(p => p.PowderNum).Width(90).Title("Number");
        c.Bound(p => p.PowderName).Width(210).Title("Description");
        c.ForeignKey(p => p.ColorId, (IEnumerable)ViewData["htmlcolors"], "ColorId", "HtmlName");
        //c.ForeignKey(p => p.VendorId, (IEnumerable<Line.OpenAccess.Vendors>)ViewData["Vendors"], "ID", "VendorName");
        c.Bound(p => p.Active).Width(60)
            .ClientTemplate("<input type='checkbox' name='Active' disabled='disabled' <#= Active? checked='checked' : '' #> />");
        c.Command(s => { s.Delete().ButtonType(GridButtonType.Image); });
        c.Bound(p => p.PowderId).ReadOnly().Hidden(true); //Hidden columns should be last
    })
   
    .ToolBar(commands => {
        commands.Insert()
            .ButtonType(GridButtonType.ImageAndText);
        commands.SubmitChanges()
            .ButtonType(GridButtonType.ImageAndText);
    })
     
    //.DataBinding(b => b.Server()
    .DataBinding(d => {
        d.Ajax()
        .Update("AjaxGridUpdates", "Powders")
        .Select("_ForeignKeyColumn", "Powders"); //VERY important
    })
     
    //Happens on client (ajax)
    .ClientEvents(e => {
        e.OnDataBinding("Grid_onDataBinding");
        e.OnError("Grid_onError");
    })
     
    .Editable(editing => editing.Mode(GridEditMode.InCell))
    .Pageable(pager => pager.PageSize(40))
    .Sortable()
    .Filterable()
    .KeyboardNavigation()
    .Scrollable(scroll => scroll.Height(500))
    .HtmlAttributes(new { style = "width:820px;" })
)

---------------------------------------------------------------------------------------------------------------------------------------
Controller
----------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Line.OpenAccess;
using Telerik.Web.Mvc;
 
namespace Line.Controllers
{
    public class PowdersController : Controller
    {
 
        LineEntities LineDB = new LineEntities();
 
 
        //View
        [GridAction]
        public ActionResult Index()
        {
            PopulateHtmlColors();
            return View();
        }
 
        /////////////////////////////////////////////////////////////////////////////////
        //   Ajax Binding Versions
        /////////////////////////////////////////////////////////////////////////////////
 
        private void PopulateHtmlColors()
        {
            //ViewData["htmlcolors"] = LineDB.Powders.ToList();
 
            ViewData["htmlcolors"] = new LineEntities().HtmlColors
                                                                                  .Select(e => new { ColorId = e.ColorId, HtmlName = e.HtmlName })
                                                                                  .OrderBy(e => e.HtmlName);
        }
 
        public ActionResult ForeignKeyColumn()
        {
            PopulateHtmlColors();
            return View();
        }
 
        [GridAction]
        public ActionResult _ForeignKeyColumn()
        {
            return View(new GridModel<Powders>
            {
                Data = this.LineDB.Powders.ToList()
            });
        }    
 
 
        public ActionResult AjaxBinding()
        {
            return View(new GridModel<HtmlColors>
            {
                Data = this.LineDB.HtmlColors.ToList()
            });
        }
 
         
        //Ajax Binding
        [GridAction]
        public ActionResult _AjaxBinding()
        {
            return View(new GridModel<Powders> {
                Data = this.LineDB.Powders.ToList()
            });
        }

4 Answers, 1 is accepted

Sort by
0
Matthew
Top achievements
Rank 1
answered on 22 Nov 2011, 02:25 AM
After further testing I've confirmed the data is making it to the view.  I added the following to the top of the view and output the contents of the database...
<ul id="colors">
 @foreach (var color in ViewData["htmlcolors"] as List<Line.OpenAccess.HtmlColors>){
     <li>
        @color.ColorId, @color.HtmlName;
    </li>
 }
</ul>
0
Rosen
Telerik team
answered on 22 Nov 2011, 09:09 AM
Hello Matthew,

Looking at the error you have mentioned it seems that System.Collections (the IEnumerable namespace) is not included on the page.

Best wishes,
Rosen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Matthew
Top achievements
Rank 1
answered on 22 Nov 2011, 04:19 PM
I added System.Collections to my controller.  It didn't help.  However, I did eventually find a syntax that would work.  I have attached my code.  My sample includes both the non-working code that matches the Telerik demo, and my functioning code commented out.
View
------------------------
@model IEnumerable<Line.OpenAccess.Parts>
 
@{
    ViewBag.Title = "Parts";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewBag.Title</h2>
 
@(Html.Telerik().Grid(Model)
    .Name("grid1")
    .DataKeys(k => k.Add(o => o.PartId))
    .Columns(c =>{
        c.Bound(h => h.PartNum).Width(90).Title("Number");
        c.Bound(h => h.Revision).Width(60).Title("Rev");
        c.Bound(h => h.PiecesPer).Width(60).Title("QTY");
        //c.ForeignKey(h => h.PowderId, ViewData["powders"] as List<Line.OpenAccess.Powders>, "PowderId", "PowderName").Width(110).Title("Powder");
        c.ForeignKey(h => h.PowderId, (IEnumerable)ViewData["powders"], "PowderId", "PowderName").Width(110).Title("Powder");
        c.Bound(h => h.LineSpeed).Width(70).Title("Speed");
 
 
Controller
-----------------------------------------------
        private void PopulateComboBoxes() {
 
            ViewData["powders"] = LineDB.Powders
                                        .Select(e => new { PowderId = e.PowderId, PowderName = e.PowderName })
                                        .OrderBy(e => e.PowderName);
 
            //IEnumerable<Powders> powders = LineDB.Powders.ToList();
            //ViewData["powders"] = powders;
        }

While I found a partial solution, my syntax does not permit me to limit the fields being sent through ViewData or apply a sort.  I would like to get the demo syntax working.

0
Rosen
Telerik team
answered on 23 Nov 2011, 09:12 AM
Hi Matthew,

I'm afraid that I do not see a System.Collections namespace included in the code snippet you have pasted? Please try the following highlighted change:

@model IEnumerable<Line.OpenAccess.Parts>
  
@{
    ViewBag.Title = "Parts";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>@ViewBag.Title</h2>
  
@(Html.Telerik().Grid(Model)
    .Name("grid1")
    .DataKeys(k => k.Add(o => o.PartId))
    .Columns(c =>{
        c.Bound(h => h.PartNum).Width(90).Title("Number");
        c.Bound(h => h.Revision).Width(60).Title("Rev");
        c.Bound(h => h.PiecesPer).Width(60).Title("QTY");
        //c.ForeignKey(h => h.PowderId, ViewData["powders"] as List<Line.OpenAccess.Powders>, "PowderId", "PowderName").Width(110).Title("Powder");
        c.ForeignKey(h => h.PowderId, (System.Collections.IEnumerable)ViewData["powders"], "PowderId", "PowderName").Width(110).Title("Powder");
        c.Bound(h => h.LineSpeed).Width(70).Title("Speed");

Regards,
Rosen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
Tags
Grid
Asked by
Matthew
Top achievements
Rank 1
Answers by
Matthew
Top achievements
Rank 1
Rosen
Telerik team
Share this question
or