Click here to Skip to main content
13,193,468 members (41,576 online)
Rate this:
Please Sign up or sign in to 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
Posted 8-Jan-13 7:26am
Updated 9-Jan-13 3:47am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
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
Zoltán Zörgő 9-Jan-13 7:54am
Good for you - this can be achieved in many ways. But I can't help you in what you want, only in finding the cause of your error. Please answer to my question above.
medloyl1 9-Jan-13 8:10am
public ObjectSet<utilisateurs> Utilisateurs
if ((_Utilisateurs == null))
_Utilisateurs = base.CreateObjectSet<utilisateurs>("Utilisateurs");
return _Utilisateurs;
private ObjectSet<utilisateurs> _Utilisateurs;

#region AddTo Methods

/// <summary>
/// Deprecated Method for adding a new object to the Utilisateurs EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead.
public void AddToUtilisateurs(Utilisateurs utilisateurs)
base.AddObject("Utilisateurs", utilisateurs);
Zoltán Zörgő 9-Jan-13 8:19am
Ok. This is not a good model, but that's not the problem. One big problem is, that you use the same name for two different things: you have MvcApplication43.Utilisateurs and you have var Utilisateur = from u in _db.Utilisateurs select u;. Now, let's hope, that _db.Utilisateurs is a collection of MvcApplication43.Utilisateurs. This way var Utilisateur will be also an IEnumerable of that type. And after those where clauses you will still have that enumeration. But your view model is a single element not an enumeration. Use @model IEnumerable<MvcApplication43.Utilisateurs>. But since you will get an enumeration, you will have to make your view process an enumeration.
medloyl1 9-Jan-13 8:30am
so i have just to put @model IEnumerable<mvcapplication43.utilisateurs> in the view??
Zoltán Zörgő 9-Jan-13 9:45am
Should be enough if there are no other problems.

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

Advertise | Privacy |
Web03 | 2.8.171018.2 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100