在 asp.net MVC 2 中组织代码时出现问题

发布于 2024-10-15 09:32:32 字数 868 浏览 2 评论 0原文

我是 MVC 新手,昨天从图书馆借了一本关于 ASP.NET MVC 2 的书。

我对模型、视图和控制器中应该包含什么样的代码有点困惑。我正在研究书中的用户输入验证,如果我理解正确的话,它们似乎在“模型”中声明变量,在“控制器”中验证用户输入并在“视图”中显示网页。

型号: -声明变量

e.g: 
class Contact:
public string Name { get; set; }
public string EmailAddress { get; set; }

视图: -包含HTML、HTML Helper代码,显示内容并使用“模型”

e.g: 
<%: Html.TextBoxFor(model => model.Name) %>
<%: Html.ValidationMessageFor(model => model.Name) %>

控制器: 中的变量 - 从模型中“播放”/“操作”变量 + 调用“View”以在最后显示网页(返回 View())。 (验证此示例的用户输入)

e.g:
if (String.IsNullOrEmpty(contact.Name))
   ModelState.AddModelError("Name", "Please enter your name.");

我不确定,但在我看来,“控制器”对我来说是“繁重的编码”部分。另一方面,“视图”是很好的旧 HTML,显示网站的标记,“模型”是存储数据的地方(例如声明变量)。

请让我知道我的方向是否正确。 谢谢 :)

I'm new to MVC and I borrow a book from library yesterday on asp.net MVC 2.

I'm a bit confused about what kind of code should be included in Model, view and controller. I was working on user input validation from the book and if I understand correctly, it seems they declare variables in "Model", validating user input in "Controller" and display the web page in "View".

Model:
-Declaring variables

e.g: 
class Contact:
public string Name { get; set; }
public string EmailAddress { get; set; }

Views:
-Contain HTML, HTML Helper code, displaying contents and use variables from "Model"

e.g: 
<%: Html.TextBoxFor(model => model.Name) %>
<%: Html.ValidationMessageFor(model => model.Name) %>

Controller:
-"Playing"/"Manipulating" variables from Model + Call "View" to display web page at the end (return View()). (Validating user input for this example)

e.g:
if (String.IsNullOrEmpty(contact.Name))
   ModelState.AddModelError("Name", "Please enter your name.");

I'm not sure but it seems to me that "Controller" is the "heavy coding" part to me. On the other hand, "View" is the good old HTML, markup that display the web site and "Model" is a place that store data (declaring variables for example.)

Please let me know whether I'm on the right direction.
Thanks :)

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

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

发布评论

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

评论(2

我为君王 2024-10-22 09:32:32

我不确定,但在我看来,“控制器”对我来说是“繁重的编码”部分

你应该避免使用胖控制器。事实上,控制器可能依赖于包含应用程序业务逻辑的服务层。该服务层本身可以依赖于在模型上执行数据访问(简单的 CRUD 操作)的存储库。因此,控制器只需在服务层上调用业务操作来获取/更新模型,然后将视图模型传递给视图以显示它。

I'm not sure but it seems to me that "Controller" is the "heavy coding" part to me

You should avoid having fat controllers. In fact the controller might depend on a service layer which contains the business logic of you application. This service layer could itself depend on repositories performing the data access (simple CRUD operations) on the models. So the controller would simply invoke a business operation call on the service layer to fetch/update the model and then pass a view model to the view in order to display it.

浊酒尽余欢 2024-10-22 09:32:32

我也是 MVC 新手,刚开始做的第一件事就是查看 Scott Hanselman 和 Phill Haack 的大部分演讲。所以你可以尝试这个:[http://channel9.msdn.com/blogs/matthijs/aspnet-mvc-2-basics-introduction-by-scott-hanselman]

[1] [1]: http://channel9.msdn.com/blogs/matthijs/ aspnet-mvc-2-basics-introduction-by-scott-hanselman 开始使用。从我得到的信息来看,达林是对的。尽量避免使用胖控制器并使用模型的存储库。另外,我注意到,对于简单的验证(必需的、最大字符串长度等),最好在模型上使用 DataAnnotion...因此您的联系人模型可能如下所示:

class Contact:
[Required]
public string Name { get; set; }
public string EmailAddress { get; set; }

这将使编辑/创建视图上需要 Name 属性。

I am also new to MVC and first thing i did when i started was to view most of the talks given by Scott Hanselman and Phill Haack. So you might try this one: [http://channel9.msdn.com/blogs/matthijs/aspnet-mvc-2-basics-introduction-by-scott-hanselman][1]

[1]: http://channel9.msdn.com/blogs/matthijs/aspnet-mvc-2-basics-introduction-by-scott-hanselman to get started. From what i got so for, Darin is right. Try to avoid fat controllers and work with a repository for your model. Also, I noticed that for simple validation (required, maximum string length and such) it is preferred to use DataAnnotion on the model... So your Contact model might look like this:

class Contact:
[Required]
public string Name { get; set; }
public string EmailAddress { get; set; }

This will make the Name property required on Edit/Create Views.

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