我有一个页面:
<%@ Page Inherits="System.Web.Mvc.View<DTOSearchResults>" %>
Run Code Online (Sandbox Code Playgroud)
在其上,以下内容:
<% Html.RenderPartial("TaskList", Model.Tasks); %>
Run Code Online (Sandbox Code Playgroud)
这是DTO对象:
public class DTOSearchResults
{
public string SearchTerm { get; set; }
public IEnumerable<Task> Tasks { get; set; }
Run Code Online (Sandbox Code Playgroud)
这是部分:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Task>>" %>
Run Code Online (Sandbox Code Playgroud)
当Model.Tasks不为null时,一切正常.但是当它为null时,我得到:
传递到字典中的模型项的类型为'DTOSearchResults',但此字典需要类型为'System.Collections.Generic.IEnumerable`1 [Task]'的模型项.
我认为它一定不知道使用哪个重载,所以我这样做(见下文)是明确的,但我仍然得到同样的问题!
<% Html.RenderPartial("TaskList", (object)Model.Tasks, null); %>
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过检查null,或者甚至不传递null来解决这个问题,但这不是重点.为什么会这样?
如果我得到了继承自的视图:
System.Web.Mvc.ViewPage<Foo>
Run Code Online (Sandbox Code Playgroud)
其中Foo有一个带有类型字符串的属性栏
和视图想要呈现强类型的局部视图,它继承自:
System.Web.Mvc.ViewUserControl<string>
Run Code Online (Sandbox Code Playgroud)
像这样:
Html.RenderPartial("_Bar", Model.Bar);%>
Run Code Online (Sandbox Code Playgroud)
那为什么会抛出这个:
传递到字典中的模型项是'Foo'类型,
但是这个字典需要一个'System.String'类型的模型项.
当bar未初始化时?
更具体:为什么它传递Foo,它应该传递null?
我正在使用MVC Preview 5开发一个应用程序.我使用了类型化的视图.
设置debug ="false"后,我发现我收到了表单错误:
"传递到字典中的模型项的类型为'blah.Models.UserAdmin.IndexData',但此字典需要类型为'blah.Models.OrganisationAdmin.IndexData'的模型项".
调试打开时,我没有收到这些错误.
看起来MVC框架是按视图名称缓存视图.如果我有两个使用相同名称的视图的操作(虽然在不同的命名空间中),我发现只有先执行的操作成功,第二个总是导致此错误.
例如,我有一个UserAdminController和一个OrganisationAdminController.
两者都有"索引"动作.
两者都使用名为'Index.aspx'的视图(每个视图都包含在控制器的视图文件夹中; Views/UserAdmin/Index.aspx和Views/OrganisationAdmin.Index.aspx).
两个视图都是类型化的,并使用名为IndexData的模型(blah.Models.UserAdmin.IndexData和blah.Models.OrganisationAdmin.IndexData)
如果我首先访问OrganisationAdmin/Index,我发现任何后续查看UserAdmin/Index的尝试都会导致上面显示的错误消息.
相反,如果我首先访问UserAdmin/Index(重新启动应用程序后),我发现导航到OrganisationAdmin/Index会导致等效错误(反之类型).
我重命名了我的一个观点"UserAdminIndex.aspx",这似乎解决了这个问题.但是,这不应该是一个问题.当然MVC框架支持类似命名的视图?我错过了什么?
任何帮助感激不尽.
沙
请注意,我已经看到了一个问题"在ASP.NET MVC中,我在使用正确的类型化对象呈现用户控件时遇到了错误的类型错误".我遇到了类似的问题,但我没有使用RenderUserControl().
堆栈跟踪:
InvalidOperationException: The model item passed into the dictionary is of type 'blah.Models.RoleAdmin.IndexData' but this dictionary requires a model item of type 'blah.Models.UserAdmin.IndexData'.]
System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +231
System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary viewDataDictionary) +99
System.Web.Mvc.ViewPage`1.SetViewData(ViewDataDictionary viewData) +60
System.Web.Mvc.WebFormView.RenderViewPage(ViewContext context, ViewPage page) +64
System.Web.Mvc.WebFormView.Render(ViewContext viewContext, TextWriter writer) +85
System.Web.Mvc.ViewResult.ExecuteResult(ControllerContext context) +206
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ActionResult actionResult) +19
System.Web.Mvc.<>c__DisplayClass12.<InvokeActionResultWithFilters>b__f() +18
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +257
System.Web.Mvc.<>c__DisplayClass14.<InvokeActionResultWithFilters>b__11() +20
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter …
Run Code Online (Sandbox Code Playgroud) 在我看来,我遇到了这个麻烦的错误:
传递到字典中的模型项的类型为"ContactWeb.Models.ContactListViewModel",但此字典需要"ContactWebLibrary.Contact"类型的模型项.
在这行代码上: @{Html.RenderPartial("Form");}
我在@model ContactWeb.Models.ContactListViewModel
这个文件的顶部使用.
这是我的观点:
@model ContactWeb.Models.ContactListViewModel
<h2>Edit</h2>
@{Html.RenderPartial("Form");}
@using (Html.BeginForm())
{
<fieldset>
<legend>Select roles for this user:</legend>
<ul>
@foreach(var role in Model.AllRoles)
{
<li><input name="Roles" type="checkbox" value="@role" />@role</li>
}
</ul>
<input type="submit" />
</fieldset>
}
Run Code Online (Sandbox Code Playgroud) 我是ASP.NET MVC的初学者,我有一个相当奇怪的问题,我似乎无法解决如何修复.我通过谷歌找到了同一问题的各种版本的多种解决方案,但它们似乎与我的问题无关.如果有人能够帮助我发现错误,我将不胜感激.
编辑:我知道将@model app.Models.Authentication更改为@model app.Models.ContactMessage将修复它,但我不明白为什么,显然提出的修复将给我一些与表单相关的其他错误.
我的错误:传入字典的模型项的类型为"app.Models.Auth",但此字典需要"app.Models.ContactMessage"类型的模型项
堆栈跟踪:
[InvalidOperationException: The model item passed into the dictionary is of type 'app.Models.Authentication', but this dictionary requires a model item of type 'app.Models.ContactMessage'.]
System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) +378
System.Web.Mvc.ViewDataDictionary.set_Model(Object value) +47
System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) +259
System.Web.Mvc.ViewDataDictionary`1..ctor(ViewDataDictionary viewDataDictionary) +37
System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) +98
System.Web.Mvc.WebViewPage.set_ViewData(ViewDataDictionary value) +39
System.Web.Mvc.WebViewPage.ConfigurePage(WebPageBase parentPage) +267
System.Web.WebPages.<>c__DisplayClass3.<RenderPageCore>b__2(TextWriter writer) +318
System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +42
System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, HelperResult content) +45
System.Web.WebPages.WebPageBase.Write(HelperResult result) +53
System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +178
System.Web.WebPages.WebPageBase.PopContext() +229
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) …
Run Code Online (Sandbox Code Playgroud)