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()
});
}