1
votes

i am new to MVC, and trying to build students marks system,

Model class looks like this:

 public partial class MarksType
 {
    public int MarksTypeId { get; set; }
    public Nullable<int> English { get; set; }
    public Nullable<int> Math { get; set; }
    public Nullable<int> Physics { get; set; }
    public Nullable<int> Chemistry { get; set; }
    public Nullable<int> ComputerScience { get; set; }
    public Nullable<int> MoralScience { get; set; }
    public Nullable<int> TotalMarks { get; set; }
  }

my controller looks like this:

public class MarksController : Controller
{
    MarksEntities marks = new MarksEntities();

public ActionResult MarksProfile()
{
   List<MarksType> yourmarks = from x in marks.MarksType where x.MarksTypeId == 5
                         group x by 1 into y
                         select new MarksType
                         {   

                             English = y.Sum(x => x.English),
                             Math = y.Sum(x => x.Math),
                             Physics = y.Sum(x => x.Physics),
                             Chemistry= y.Sum(x => x.Chemistry),
                             ComputerScience = y.Sum(x => x.ComputerScience),
                             MoralScience= y.Sum(x => x.MoralScience),

                             TotalMarks = y.Sum(x => x.English) + y.Sum(x => x.Math)
                             + y.Sum(x => x.Physics) + y.Sum(x => x. Chemistry)
                             +y.Sum(x => x.ComputerScience) + y.Sum(x => x.MoralScience)
                         }).ToList();



        return View(yourmarks);
    }
}

Finally my View Looks Like this:

@model IEnumerable<MvcMarks.Models.MarksType>

<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
    <th>
        @Html.DisplayNameFor(model => model. MarksTypeId)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.English)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Math)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Physics )
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Chemistry)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.ComputerScience)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.MoralScience)
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.MarksTypeId)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.English)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Math)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Physics )
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Chemistry)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ComputerScience)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.MoralScience)
    </td>

    </tr>
    }

    </table>

i get this error:

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType21[System.Nu>llable1[System.Int32]]]',but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MvcMarks.Models.MarksType]'.

i created View by right clicking on MarksProfile Action Method and checked strongly typed view with MarksType Model selected , selected scaffold template to List ,and finally checked create as a partial view

may i know what i am doing wrong

ANY help would be great.

3
Your query is returning a collection of anonymous objects (select new {..})user3559349

3 Answers

2
votes

You are passing an anonymous type to your view, but it expects an IEnumerable of MarksType, something like this:-

List<MarksType> yourmarks = (from x in marks.MarksType where x.MarksTypeId == 5
                         group x by 1 into y
                         select new MarksType
                         {      
                             MarksTypeId = .. ,
                             English = ..       
                             TotalMarks = y.Sum(x => x.English) +y.Sum(x => x.Math)
                             + y.Sum(x => x.Physics) + y.Sum(x => x. Chemistry)
                             +y.Sum(x => x.ComputerScience) + y.Sum(x => x.MoralScience)
                         }).ToList();

But, since your Model MarksTypeId does not contain a definition of MarksType, you will have to add this property to your model:-

public partial class MarksType
{
    //other properties
    public decimal TotalMarks {get; set; }
}
1
votes

As the exception states, your code is expecting other type in the Model than you've provided:

@model IEnumerable<MvcMarks.Models.MarksType>

Check the yourmarks object, and change the View accordingly. As I can see, you are creating the query with total marks, not with MarksType objects, and your code isn't working because of this.

And why are you need the TotalMarks property? You doesn't use it in your View.

0
votes

Create a new View Model and use it instead of MarksType

 public partial class MarksTypeViewModel
 {
    public Nullable<int> English { get; set; }
    public Nullable<int> Math { get; set; }
    public Nullable<int> Physics { get; set; }
    public Nullable<int> Chemistry { get; set; }
    public Nullable<int> ComputerScience { get; set; }
    public Nullable<int> MoralScience { get; set; }
    public Nullable<int> TotalMarks { get; set; }
 }

Make sure that i haven't added MarksTypeID in this model. You should avoid some unnecessary fields that is not being used in your View. for example CreateDate, UpdateDate, etc.

Now, you can use that view model in your linq statement instead of MarksType

List<MarksTypeViewModel> yourmarks = from x in marks.MarksType where x.MarksTypeId == 5
                     group x by 1 into y
                     select new MarksTypeViewModel
                     {   
                         English = y.Sum(x => x.English),
                         Math = y.Sum(x => x.Math),
                         Physics = y.Sum(x => x.Physics),
                         Chemistry= y.Sum(x => x.Chemistry),
                         ComputerScience = y.Sum(x => x.ComputerScience),
                         MoralScience= y.Sum(x => x.MoralScience),

                         TotalMarks = y.Sum(x => x.English) + y.Sum(x => x.Math)
                         + y.Sum(x => x.Physics) + y.Sum(x => x. Chemistry)
                         +y.Sum(x => x.ComputerScience) + y.Sum(x => x.MoralScience)
                     }).ToList();

and modify the model binding in your view page with your new ViewModel. Its called MVVM patern.

@model IEnumerable<MvcMarks.Models.MarksTypeViewModel>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>