在 asp.net MVC 2 中组织代码时出现问题
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你应该避免使用胖控制器。事实上,控制器可能依赖于包含应用程序业务逻辑的服务层。该服务层本身可以依赖于在模型上执行数据访问(简单的 CRUD 操作)的存储库。因此,控制器只需在服务层上调用业务操作来获取/更新模型,然后将视图模型传递给视图以显示它。
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.
我也是 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...因此您的联系人模型可能如下所示:
这将使编辑/创建视图上需要 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:
This will make the Name property required on Edit/Create Views.