Visual Studio 2010的MVC中区分模型、视图和控制器

发布于 2024-12-25 02:39:37 字数 49 浏览 1 评论 0原文

我阅读了 MVC 概念,但我不明白哪种类型的代码应该在控制中,哪种类型应该在模型中。

I read MVC concept, but i dont understand which type of code should be in controll and which one in model.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

淡淡の花香 2025-01-01 02:39:37

我想你能理解什么是视图。 view 负责显示或 UI。

模型是您定义要在应用程序中作为实体工作的类的部分。

控制器是您编写程序逻辑的类。因此,控制器将使用模型向视图提供数据,使用模型从视图检索数据并执行您编程所需的任何任务。

例如,如果该程序用于图书馆系统,您可以为图书创建一个模型类,如下所示,

public class BookViewModel
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Publisher {get;set;}
        public string ISBN {get;set;}
    }

控制器类具有查看图书详细信息、创建新书等操作。以下是

public class BooksController : Controller
    {
       //
        // GET: /Books/Details/5
        [HttpGet]
        public ActionResult Details(int id)
        {
           // your logic goes here.
            return View(bookViewModel);
        }

      [HttpPost]
        public ActionResult Create( BookViewModel bookViewModel)
        {
            // your logic goes here
        }
    }

我的 示例我想这会帮助你理解这个概念

干杯。

贾扬加。

I thing you can understand what is a view. view is responsible of the display or UI.

Model is the part where you define the classes which you want to work as entities in your application.

Controller is the class which you write your program logic. therefore a controller will provide data to the views using Models, retrieve data from views using Model and perform any task that you program wants.

for example if the program is for a library system you can have a Model class for Book which could look like the following

public class BookViewModel
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Publisher {get;set;}
        public string ISBN {get;set;}
    }

the controller class has the operations like View details about the book, Create new book, etc.. the following is an example

public class BooksController : Controller
    {
       //
        // GET: /Books/Details/5
        [HttpGet]
        public ActionResult Details(int id)
        {
           // your logic goes here.
            return View(bookViewModel);
        }

      [HttpPost]
        public ActionResult Create( BookViewModel bookViewModel)
        {
            // your logic goes here
        }
    }

I think this will help you a little to understand the concept

Cheers.

Jayanga.

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