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

Begginer on Telerik and MVC, Grid with IQueryable

3 Answers 124 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Emir
Top achievements
Rank 1
Emir asked on 15 Mar 2012, 07:17 AM

Hi,

I was doing doing the instruction video for the Grid and I am interested to get server binding with iqueryable to work. This is what i did:

DBContextClass

namespace MvcApplication1.Models
{
    public class MvcRulesContext : DbContext
    {
        public virtual DbSet<Rule> Rules { get; set; }
        public virtual DbSet<User> Users { get; set; }
    }
}

My controller class

...
    public class RulesController : Controller
    {
        private MvcRulesContext db = new MvcRulesContext();
  
        //
        // GET: /Rules/
  
        public ActionResult Index()
        {
            return View(db.Rules.Select(o => new MvcApplication1.Models.Rule
            {
                ID = o.ID,
                Permission = o.Permission
            }) );
        }
...

My strongly typed view

@model System.Data.Linq.Table<MvcApplication1.Models.Rule>
@{
    ViewBag.Title = "Index";
}
  
<h2>Index</h2>
  
<p>
    @Html.ActionLink("Create New", "Create")
</p>
@{
 Html.Telerik().Grid(Model).Name("grid1").Sortable().Render();   
 }

What happens is that i get this error:

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[MvcApplication1.Models.Rule]', but this dictionary requires a model item of type 'System.Data.Linq.Table`1[MvcApplication1.Models.Rule]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[MvcApplication1.Models.Rule]', but this dictionary requires a model item of type 'System.Data.Linq.Table`1[MvcApplication1.Models.Rule]'.

Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace:

[InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[MvcApplication1.Models.Rule]', but this dictionary requires a model item of type 'System.Data.Linq.Table`1[MvcApplication1.Models.Rule]'.]
System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +531765
System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +373
System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +48
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +99
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +295
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +248
System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +21
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +326
System.Web.Mvc.Controller.ExecuteCore() +109
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +91
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +34
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +13
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +48
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +10303829
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +178

Please help, I am stuck with this and google is not my friend.

3 Answers, 1 is accepted

Sort by
0
Dadv
Top achievements
Rank 1
answered on 21 Mar 2012, 12:06 PM
Try to change 
@model System.Data.Linq.Table<MvcApplication1.Models.Rule> 

to
@model IEnumerable<MvcApplication1.Models.Rule> 
0
Emir
Top achievements
Rank 1
answered on 21 Mar 2012, 12:40 PM
Sure that works. I have done that but in the grid instruction video he was talking that if you want to get only data that you see instead of getting everything you should use IQueryable. Not sure if that is still true. Confusing.
0
Dadv
Top achievements
Rank 1
answered on 21 Mar 2012, 02:22 PM
@model IQueryable<MvcApplication1.Models.Rule>  
should work too
Tags
General Discussions
Asked by
Emir
Top achievements
Rank 1
Answers by
Dadv
Top achievements
Rank 1
Emir
Top achievements
Rank 1
Share this question
or