强类型视图差异(MVC 源代码与程序集)
创建强类型部分视图
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Pt.Data.Services>>" %>
<table>
<% foreach (Pt.Data.Services item in Model)
{ Html.RenderPartial("ServiceItem",item); } %>
</table>
我正在尝试在控制器中
IEnumerable<Services> Model=null;
using (tl ctx = new tl(Config.ConnectionString))
{
Model = ctx.Services.ToList();
}
return View("List",Model);
:在引用二进制程序集 System.Web.Mvc 的项目中运行时,此方法效果很好。
但是,如果我删除二进制程序集并添加带有 MVC 源代码的项目进行调试,它就会停止识别强类型视图。
它的工作方式类似于 ViewPage
而不是 ViewPage
结果我收到错误:
编译器错误消息:CS1579:foreach 语句无法对“object”类型的变量进行操作,因为“object”不包含“GetEnumerator”的公共定义`
为什么这适用于已编译的 MVC,但不适用于源代码? 我怎样才能使源正确运行?
I'm trying to create a strongly typed partial view
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Pt.Data.Services>>" %>
<table>
<% foreach (Pt.Data.Services item in Model)
{ Html.RenderPartial("ServiceItem",item); } %>
</table>
in the Controller:
IEnumerable<Services> Model=null;
using (tl ctx = new tl(Config.ConnectionString))
{
Model = ctx.Services.ToList();
}
return View("List",Model);
This workied well when running in a project with the binary assembly System.Web.Mvc
referenced.
But if I remove binary assembly and add a project with MVC sources for debugging, it stops recognizing strongly typed views.
It's working like a ViewPage
instead of ViewPage<TModel>
As result I'm getting the error:
Compiler Error Message: CS1579: foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'`
Why would this work with the compiled MVC, but not with the sources? And how can I make the sources run correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否将 ~/Views/Web.config: 中的这一行更改
为这样?:
实际上这是 Steve Sanderson 的 帖子 可能会有所帮助
Have you changed this line in ~/Views/Web.config:
to this?:
Actually this Steve Sanderson's post might be helpful
我不知道可能导致引用的源项目的行为与其自己的构建输出(程序集)不同的原因。 我仍然可以推荐:
1- 确保您使用的源与构建程序集的源相同。
2- 确保添加了对源项目的引用。
3- RC 您的解决方案文件,选择干净的解决方案,然后重建并重试。
I don't know of a reason that might cause a referenced source project to behave differently than its own build output(assembly). Still I can recommend:
1- Make sure the source you're using is the same the assembly was built off.
2- Make sure you added a reference to the source project.
3- RC on your solution file, choose clean solution, then rebuild and try again.