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";
}
<h2>SearchIndex</h2>
@using( Html.BeginForm("SearchIndex","Users",FormMethod.Get)){
<p>
Initial:@Html.TextBox("SearchString")
<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;
initialList.AddRange(query.Distinct());
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));
}
if
(string.IsNullOrEmpty(ByInitial))
return View(Utilisateur);
else
{
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