[英]Strongly typed MVC View Model
I am trying to strongly type my MVC View Model that is rendered as a ASP.NET ASPX page. 我试图强烈键入呈现为ASP.NET ASPX页面的MVC视图模型。 Everything is in one big project. 一切都在一个大项目中。 However, it say it can't find the name space, what am I doing wrong? 但是,它说找不到名称空间,我在做什么错?
Here is the error: 这是错误:
CS0234: The type or namespace name 'MyModel' does not exist in the namespace
'MyProject.Models' (are you missing an assembly reference?)
Here is my custom Model: 这是我的自定义模型:
namespace MyProject.Models {
public class MyModel{
public string companyID { get; set; }
public MyModel(string companyID){
this.companyID = companyID;
}
}
}
Here is my Strongly Typed View rendered in ASPX, with the filename MyStronglyTypedView.aspx: 这是我在ASPX中呈现的“强类型视图”,文件名为MyStronglyTypedView.aspx:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MyProject.Models.MyModel>" %>
<%@ import namespace='MyProject.Models' %>
.....
<script runat="server">
protected void Page_Load(object sender, EventArgs e) {
var curCompany = Model.companyID;
}
</script>
....
And here is how I am calling the page and passing my model to it (I am calling it on a separate Razor page as a partial): 这就是我调用页面并将模型传递给它的方式(我在一个单独的Razor页面上将其作为部分调用):
@Html.Partial("MyStronglyTypedView",new MyProject.Models.MyModel("1"))
The error you are experiencing occurs because of incorrect Page
directive Inherits
attribute value. 由于错误的Page
指令Inherits
attribute值而发生您遇到的错误。 Directives are part of the ASP.NET markup and thus are not language-specific. 指令是ASP.NET标记的一部分,因此不是特定于语言的。 For example 例如
System.Web.Mvc.ViewPage<MyProject.Models.MyModel>
is how you specify generic type in C#, but say in VB.NET it would be 是在C#中指定通用类型的方法,但是在VB.NET中会这样
System.Web.Mvc.ViewPage(of MyProject.Models.MyModel)
All in all, types here should be specified in CLR notation. 总而言之,此处的类型应以CLR表示法指定。 In your case it might look something like 您的情况可能看起来像
System.Web.Mvc.ViewPage`1[[MyProject.Models.MyModel, MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
This is so called Fully Qualified Type Name. 这就是所谓的完全合格类型名称。 To see this name for a particular type, one can use Type.FullName
property: 要查看特定类型的名称,可以使用Type.FullName
属性:
Type targetType = typeof(System.Web.Mvc.ViewPage<MyProject.Models.MyModel>);
string name = targetType.FullName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.