MVC3 ViewModel 命名空间问题

发布于 2024-11-29 14:35:27 字数 682 浏览 0 评论 0原文

我正在尝试创建一个 ViewModel 类。在“ViewModels”文件夹中创建类后。我的列表类型声明无法识别。我的问题和代码如下:

  1. 是否有创建 ViewModel 类的特殊方法?
  2. 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:

  1. Are there some special way of creating ViewModel Classes?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾冷 2024-12-06 14:35:27

是否有一些特殊的方法来创建 ViewModel 类?

,像创建任何其他类一样创建它们。 惯例是将它们放在Models文件夹中。

ViewModel 是一种方法,而不是 MVC3 中的一项功能吗?

有点。它们不是框架本身的功能,而是保持视图简单干净并简化模型绑定的建议。

有人可以告诉我我错过了什么吗,谢谢

你的班级声明在哪里?

namespace MvcApplication8.ViewModels
{
   public class ThisIsTheClassNameAndMustGoFirst

Are there some special way of creating ViewModel Classes?

No, create them like creating any other class. The convention is to place them in the Models folder.

Are ViewModels a methodology, rather than a feature in MVC3?

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.

Can someone please tell me what I have missed thanks

Where's your class declaration?

namespace MvcApplication8.ViewModels
{
   public class ThisIsTheClassNameAndMustGoFirst
风轻花落早 2024-12-06 14:35:27
  1. 使用 Razor 创建一个新的空 MVC 3 项目。
  2. 在Models文件夹下添加一个类定义,即:

    命名空间 MvcApplication1.Models
    {
        公共类WhateverNameYouWantModel
        {
            公共字符串 Foo { 得到;放; }
            公共字符串酒吧{获取;放; }
        }
    }
    
  3. 右键单击Controllers文件夹并添加一个新控制器。名称必须以“Controller”结尾。不必费心检查添加操作方法的选项。控制器看起来像这样:

    使用 System.Web.Mvc;
    使用 MvcApplication1.Models;
    
    命名空间 MvcApplication1.Controllers
    {
        公共类HelloController:控制器
        {
            公共 ActionResult Index()
            {
                返回视图(新的WhateverNameYouWantModel());
            }
        }
    }
    
  4. 右键单击上面的 Index() 签名并选择“添加视图”。确保未检查任何内容,视图名称与操作名称“Index”匹配,并且 Razor 是引擎。在顶部添加模型类型:

    @model MvcApplication1.Models.WhateverNameYouWantModel
    @{
        布局=空;
    }
    
    
    <头>
        <标题>索引
    
    <正文>
        
    你好世界!
  5. 将mvc项目设置为启动项目,按F5,浏览器将打开http:// /localhost:xxxx 现在您需要指向 http://localhost:xxxx/Hello/Index< /a>

在 asp.net mvc 中,名称是视图、操作和控制器之间的所有内容。这都是惯例,你不需要遵守它,但如果你不这样做,你将不得不做一些额外的管道工作。

  1. create a new empty MVC 3 project, using Razor.
  2. add a class definition under Models folder, i.e:

    namespace MvcApplication1.Models
    {
        public class WhateverNameYouWantModel
        {
            public string Foo { get; set; }
            public string Bar { get; set; }
        }
    }
    
  3. 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:

    using System.Web.Mvc;
    using MvcApplication1.Models;
    
    namespace MvcApplication1.Controllers
    {
        public class HelloController : Controller
        {
            public ActionResult Index()
            {
                return View(new WhateverNameYouWantModel());
            }
        }
    }
    
  4. 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:

    @model MvcApplication1.Models.WhateverNameYouWantModel
    @{
        Layout = null;
    }
    <!DOCTYPE html>
    <html>
    <head>
        <title>Index</title>
    </head>
    <body>
        <div>hello world!</div>
    </body>
    </html>
    
  5. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文