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.DbQuery
1[<>f__AnonymousType2
1[System.Nu>llable1[System.Int32]]]',but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[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.
select new {..})
– user3559349