I’m working on an MVC application that at this point should return Distinct users who have made an entry in the SQL table. I have a column in the Table called EnteredBy that contains names off all users (about 10 different users) who have made entry multiple times. I have tried this LINQ query but it is not working:
[HandleError]
public ActionResult DisplayCount()
{
var enteredBy = (from user in db.Table
select user.EnteredBy).Distinct();
ViewBag.Count = enteredBy.Count();
return View(enteredBy);
}
I also tried this as suggested by someone else:
[HandleError]
public ActionResult DisplayCount()
{
var enteredBy = (from user in db.Table
select new { user.EnteredBy}).Distinct();
ViewBag.Count = enteredBy.Count();
return View(enteredBy);
}
View:
@model IEnumerable<Statistic.Models.Table>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>DisplayCount</title>
</head>
<body>
<div>
Entered by: <br /><br />
@foreach (var m in Model)
{
@m.EnteredBy <br />
}
<br /> Total Count: <br />
@ViewBag.Count
</div>
</body>
</html>
But it yields the same result:
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.
Is there something I’m missing?
Stack Trace:
[InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[<>f__AnonymousType4`1[System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Statistic.Models.DashboardLibAnswer]'.]
System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +175
System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +361
System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +49
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +98
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +23
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +245
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +22
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +176
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +75
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +99
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629296
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
The problem is that you are doing this:
It appears that your view page does not expect this type of model.
If you are storing the number of users in ViewBag, then you don’t need to pass the query to the model:
Then in your view you can display it like so:
If you look at your view (DisplayCount.cshtml), you should check what you have for your @model statement. If you’re using ViewBag, you don’t even need it. If you want to switch to a strongly typed model, you could use int instead, since that’s all you really want at this point is a count of users:
edit
Looks like you are trying to get the list of users, not a count of users. Try this:
Your view will need an IEnumerable: