本文介绍了字典需要类型的模型项。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
传递到字典中的模型项的类型为''System.Data.Objects.ObjectQuery`1 [MvcApplication43.Utilisateurs]'',但此字典需要类型为...的模型项目。
这是视图的代码:
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
推荐答案
这篇关于字典需要类型的模型项。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!