MVC3 ViewModel 命名空间问题
我正在尝试创建一个 ViewModel 类。在“ViewModels”文件夹中创建类后。我的列表类型声明无法识别。我的问题和代码如下:
- 是否有创建 ViewModel 类的特殊方法?
- ViewModel 是一种方法论,而不是 MVC3 中的一项功能吗?
有人可以告诉我我错过了什么吗谢谢-P
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication8.ViewModels
{
//compiler does not recongize List class or SelectListitem
private List<SelectListItem> _products = new List<SelectListItem>();
//compiler does not recongize List class
public List<SelectListItem> products
{
get { return _products; }
}
}
I am trying to create a ViewModel class. After I created class in "ViewModels" folder. My List type declaration are not recognized. My questions and code is below:
- Are there some special way of creating ViewModel Classes?
- Are ViewModels a methodology, rather than a feature in MVC3?
Can someone please tell me what I have missed thanks -P
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication8.ViewModels
{
//compiler does not recongize List class or SelectListitem
private List<SelectListItem> _products = new List<SelectListItem>();
//compiler does not recongize List class
public List<SelectListItem> products
{
get { return _products; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
否,像创建任何其他类一样创建它们。 惯例是将它们放在Models文件夹中。
有点。它们不是框架本身的功能,而是保持视图简单干净并简化模型绑定的建议。
你的班级声明在哪里?
No, create them like creating any other class. The convention is to place them in the Models folder.
Kind of. They're not a feature of the framework itself, but a recommendation to keep your View's simple and clean, and simplify model binding.
Where's your class declaration?
在Models文件夹下添加一个类定义,即:
右键单击Controllers文件夹并添加一个新控制器。名称必须以“Controller”结尾。不必费心检查添加操作方法的选项。控制器看起来像这样:
右键单击上面的 Index() 签名并选择“添加视图”。确保未检查任何内容,视图名称与操作名称“Index”匹配,并且 Razor 是引擎。在顶部添加模型类型:
将mvc项目设置为启动项目,按F5,浏览器将打开http:// /localhost:xxxx 现在您需要指向 http://localhost:xxxx/Hello/Index< /a>
在 asp.net mvc 中,名称是视图、操作和控制器之间的所有内容。这都是惯例,你不需要遵守它,但如果你不这样做,你将不得不做一些额外的管道工作。
add a class definition under Models folder, i.e:
right click on Controllers folder and add a new controller. the name must end with "Controller". don't bother checking option to add action methods. the controller would look like this:
right click on the Index() signature above and choose "Add View". Make sure nothing is checked, the view name matches the action name "Index" and Razor is the engine. add the model type at the top:
set the mvc project as your startup project, press F5, the browser will open to http://localhost:xxxx now you will need to point to http://localhost:xxxx/Hello/Index
In asp.net mvc names are everything between the views, actions and controllers. Its all convention, you don't need to stick to it but if you don't you are gonna have to do some extra plumbing.