Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:41:54+00:00 2026-06-14T19:41:54+00:00

I have created a MVC3 application, which uses WebGrid to populate the data from

  • 0

I have created a MVC3 application, which uses WebGrid to populate the data from the database.

My database has some columns. Say

Id | FullName|Phone|Email

I can able to sort the Id column. But if i try to sort the other columns, it is giving the following error

Unable to cast the type ‘MvcApp.Models.Student’ to type ‘MvcApp.Models.Student’. LINQ to Entities only supports casting EDM primitive or enumeration types.

I tried to debug and see where the error is, i found that the sorting is perfect, but when it returns the data to view it gives error

My View Code:

@{
    ViewBag.Title = "listStudents";
    Layout = "~/Views/Shared/_Layout.cshtml";
    WebGrid grid = new WebGrid(Model, canPage: true, canSort: true, rowsPerPage: 3); 
}

@grid.Pager(WebGridPagerModes.NextPrevious)
        @grid.GetHtml(  //Error at this line
        htmlAttributes: new { id = "grdListStudents" },
    fillEmptyRows: true,
    headerStyle: "tblHeader",
    tableStyle: "tablestyle",
    mode: WebGridPagerModes.All,
    firstText: "<< First",
    previousText: "< Previous", nextText: "Next >",
    lastText: "Last >>",
    columns: new[]{
        grid.Column("intID","SId",canSort:true),
        grid.Column("strFirstName","Name",canSort:true,format:(item)=>item.strFirstName+"   "+item.strLastName),
        grid.Column("strPhone","Phone",canSort:true),
        grid.Column("strEmail","Email",canSort:true),
    }
    )

Here is my code in controller:

public readonly IStudentInfo _istudentrepository; 

public studentController( IStudentInfo _iStudentRepository)
{
    this._istudentrepository = _iStudentRepository;
}

public ActionResult listBidder(string sort, string sortdir, int? page)
{
    int startPage = 0;
    IEnumerable<Students> sList;
    if (page.HasValue && page.Value > 0)
    {
        startPage = page.Value;
    }
    sList = _istudentrepository.GetList(startPage, PageSize, sort, sortdir);
    return View(sList);                
}

Code in Interface IStudentInfo:

public interface IStudentInfo
{
    IEnumerable<Students> GetList(int intPage, int intRecords, string strSort, string sortdir);            
}

Code in Model:

private MyEntity _entity;

public StudentListRepository(MyEntity Ent)
{
    this._entity = Ent;
}
public IEnumerable<Students> GetList(int intPage, int intRecords, string strSort, string sortdir)
{

    var finalresult = new Students();
    var bidList = (from userInfo in _entity.tbl_UserInf
                   join user in _entity.tbl_User on userInfo.UserId equals user.UserId
                   select new Students()
                   {
                       intID=user.UserId,
                       strFirstName = user.FirstName,
                       strEmail = userInfo.EmailId,
                       intPhone=userInfo.Phone
                   }).OrderByDescending(m => m.intID);
    finalresult.TotalResult = bidList.Count();
    switch (strSort)
    {
        case "intID":
            if (sortdir == "ASC")
            {
                sList = sList.OrderBy(r => r.Id);
            }
            else
            {
                sList= sList.OrderByDescending(r => r.Id);
            }
            break;
        case "strFirstName":
            if (sortdir == "ASC")
            {
                sList = sList.OrderBy(r => r.strFirstName);
            }
            else
            {
                sList= sList.OrderByDescending(r => r.strFirstName);
            }
            break;
        case "strEmail":
            if (sortdir == "ASC")
            {
                sList = sList.OrderBy(r => r.strEmail);
            }
            else
            {
                sList= sList.OrderByDescending(r => r.strEmail);
            }
            break;
        //repeat same for phone
    }

    finalresult.lstStudents = sList.Skip(intPage * intRecords)
                                   .Take(intRecords)
                                   .ToList();
    return sList.AsEnumerable();
}

What is wrong in my code ? Please tell me if anyone has idea of what is going on.

Please help

thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:41:56+00:00Added an answer on June 14, 2026 at 7:41 pm

    Try to use:

    public ActionResult listBidder(string sort, string sortdir, int? page)
    {
            int startPage = 0;
            if (page.HasValue && page.Value > 0)
            {
                startPage = page.Value;
            }
            var sList = _istudentrepository.GetList(startPage, PageSize, sort, sortdir);
            return View(sList);
    }
    
    public IEnumerable<Students> GetList(int intPage, int intRecords, string strSort, string sortdir)
    {
    
            var finalresult = new Students();
            var bidList = (from userInfo in _entity.tbl_UserInf
                           join user in _entity.tbl_User on userInfo.UserId equals user.UserId
                           select new Students()
                           {
               intID=user.UserId,
                               strFirstName = user.FirstName,
                               strEmail = userInfo.EmailId,
                               intPhone=userInfo.Phone
                           }).OrderByDescending(m => m.intID);
           finalresult.TotalResult = bidList.Count();
           // There are some sorting and ordering
           finalresult.lstStudents = sList.Skip(intPage * intRecords).Take(intRecords).ToList();
           return bidList.ToArray();
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created one MVC3 application. In which I'm selecting two Ids from two
i have created an mvc3 web application that uses forms based authentication. one part
I have an MVC 3 application which uses asp.net authentication. I have just created
I have a web application (ASP.NET MVC3) which uses the jquery ui tab control
I have created a mvc3 application which work fine locally. I work for a
I am working on asp.net MVC3 application and I have created a layout.cshtml which
I am creating application in MVC3. I have a ConferenceController which has a Create()
I have an asp.net mvc3 C# application. It uses a database. The database supports
I have created mvc3 application. I have one .edmx already created which is based
I have an ASP.NET MVC 3 application which has a post action called Create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.