Click here to Skip to main content
15,849,487 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1[MvcApplication43.Utilisateurs]', but this dictionary requires a model item of type...

this is the view's code:
@model MvcApplication43.Utilisateurs
    ViewBag.Title = "SearchIndex";

 @using( Html.BeginForm("SearchIndex","Users",FormMethod.Get)){
  <input type="submit" value="Chercher" /></p>

controller's codes:
public ActionResult SearchIndex(string ByInitial , string SearchString)
               var initialList = new List<string>();
               var query = from d in _db.Utilisateurs orderby d.Initial select d.Initial;
               ViewBag.ByInitial=new SelectList(initialList);

               var Utilisateur = from u in _db.Utilisateurs select u;
               if (!String.IsNullOrEmpty(SearchString))
                   Utilisateur = Utilisateur.Where(s => s.Initial.Contains(SearchString));


                    return View(Utilisateur);

                    return View(Utilisateur.Where(x => x.Initial == ByInitial));


and this is the exception:
The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1[MvcApplication43.Utilisateurs]', but this dictionary requires a model item of type 'MvcApplication43.Utilisateurs'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1[MvcApplication43.Utilisateurs]', but this dictionary requires a model item of type 'MvcApplication43.Utilisateurs'.

Source Error:

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.

Stack Trace:

[InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Objects.ObjectQuery`1[MvcApplication43.Utilisateurs]', but this dictionary requires a model item of type 'MvcApplication43.Utilisateurs'.]
   System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +399739
   System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +377
   System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +48
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +98
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
   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) +260
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +92
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +126
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +45
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +25
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +61
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +25
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +49
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +25
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +49
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8862381
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
Updated 9-Jan-13 4:47am

1 solution

Your model defined on the view is of only one element. But the object you pass to the view is a result of a query that can have zero or more elements as result.
So you either set the model to @model IEnumerable<MvcApplication43.Utilisateurs>, or use SindleOrDefault or First instead of Where. But it depends on what you want to achieve.
Share this answer
medloyl1 8-Jan-13 13:34pm    
explain more plz!!!
Zoltán Zörgő 8-Jan-13 13:35pm    
I can not explain it more. You pass an enumeration of entities to the view, the view is created to accept a single entity only.
medloyl1 9-Jan-13 6:45am    
i try it but nothing has changed
Zoltán Zörgő 9-Jan-13 7:17am    
What exactly have you tried? Let's take slow and easy:
1) What is MvcApplication43.Utilisateurs? Paste it's declaration here.
medloyl1 9-Jan-13 7:37am    
I want to display data in a table from the user table with a text field that allow me to search users 'ByInitial' from this list

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900