开发者

Populating a Telerik Grid using ViewModel (MVC3)

开发者 https://www.devze.com 2023-03-02 20:59 出处:网络
Ok, this is a really newbie question but I am stumped. I am trying to use a ViewModel to get my data from an entity object and populate the telerik mvc grid.

Ok, this is a really newbie question but I am stumped. I am trying to use a ViewModel to get my data from an entity object and populate the telerik mvc grid.

At this point I am a bit confused and need your help.

I understand the error message but I am not sure how I need to fix this since I am really new to MVC.

----ERROR----

Error 1 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?) ProjectRepository.cs 23 20 MvcMyAPP

I have this viewmodel:

--VIEWMODEL--

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;

namespace mvcMyModel.ViewModels
{
    public class ProjectViewModel
    {
        public IQueryable<mvcMyAPP.Models.ProjectRepository> ProjectList
   开发者_开发问答     {
            get;
            set;
        }
    }
}

--CONTROLLER--

namespace MvcMyAPP.Controllers
{
    public class HomeController : Controller
    {
        // GET: /Home/
        ProjectRepository Repository = new ProjectRepository();

        public ActionResult Index()
        {
            ProjectViewModel objProjectViewModel = new ProjectViewModel();
            objProjectViewModel.ProjectList = Repository.GetProjects();
            return View(objProjectViewModel);
            return View();
        }
    }
}

----REPOSITORY (MODEL)-----

namespace mvcMyAPP.Models
{
    public class ProjectRepository
    {

        mvcMyAPP.Models.MYEntities MYDB = new MYEntities();
        //Fetching data from table

        public IQueryable<mvcMyAPP.ViewModels.ProjectViewModel> GetProjects()
        {
            var vProjects = (from tblProjects in MYDB.Projects
                                select tblProjects);
            return vProjects;
        }
    }

---GRID---

@{Html.Telerik().Grid(Model.ProjectList)
.Name(
"Grid") 
.Pageable()
.Sortable()
.Filterable()
.Groupable()
.Render();
}


You don't need the second return View() in the controller, but that isn't causing any problems.

You might try putting a cast on the return variable from your repository so it is returning the type specified in return:

 // return vProjects;

 // cast the return variable to return type
 return (IQueryable<mvcMyAPP.ViewModels.ProjectViewModel>)vProjects;

I highly recommend Microsoft site for learning MVC, they have a great starting point for learning MVC.

 http://www.asp.net/mvc
0

精彩评论

暂无评论...
验证码 换一张
取 消