ASP.NET MVC 验证 - LINQ to SQL 和视图模型 - 对正确流程的一点帮助
我正在努力找出执行某件事的最佳方式。
我正在使用 ASP.NET MVC 和 LINQ to SQL 创建一个非常简单的论坛。非常简单意味着它没有类别,每条消息由主题、作者和消息内容组成。评论可以串联,但只能深入一层(如果您评论子消息,它将显示为好像您评论了该子消息的父消息)。
哦,我还正确地对论坛进行了分页。
现在,我试图理解 ASP.NET MVC 流程:
当有人访问 Home/Forum/{page}
时,我为包含 2 个对象的视图创建一个模型 - 所有对象的列表要显示的消息(按照父母和孩子的正确顺序。我在服务器端创建列表)和一个 forumMessage newMessage
对象,因此如果用户创建新消息或回复现有消息,我可以轻松填充对象并将其发送回服务器。
然而,在阅读了大量关于验证、模型等的文章后,在我看来,使用由用户填写的分解消息字段会更好(即:< code>String messageComposer; String messageComposer;),因为为它们定义验证规则会更容易。
这有两个原因:
1) 由于 forumMessage
对象是使用 LINQ to SQL 创建的,因此定义其验证规则有点问题(是的,我知道您可以使用 好友类 ,但我有点害怕 O_o )
,似乎更大的原因 2)在所有验证示例中,如果需要验证的数据作为对象传递(即 forumMessage
),那么它是模型中唯一的对象。我没有看到模型由数据组成的场景,这些数据也只是用于填充视图(在我的例子中 - 特定页面的论坛消息)。
这让我想到了这一点:
到目前为止,每当我必须从用户那里获取输入时,我都会传递我想要显示或填充的对象作为实际的 LINQ to SQL 对象。
< strong>我的处理方式是错误的吗?我是否应该将 LINQ to SQL 对象(即类)传递用于显示目的,但使用普通的旧字段(字符串、整数等)作为我想要检索的数据,并创建实际的 LING to SQL 对象服务器端,当我得到所有数据返回控制器?
我正处于十字路口,希望得到经验丰富的老手的帮助:-)
谢谢!
I'm trying to grasp my head around the best way to carry out something.
I'm creating a very simple forum, using ASP.NET MVC and LINQ to SQL. Very simple means it has no categories, and each message consists of a subject, a composer and the message contents. Comments can be threaded, but only go one level deep (if you comment on a child message, it will appear as though you commented on the parent message of that child).
Oh, I also properly paginated the forum.
Now, the ASP.NET MVC flow bit I'm trying to understand:
When someone goes to Home/Forum/{page}
I create a model for the view with 2 objects - a list of all the messages to show (in the proper order of parents and children. I create the list server-side), and a forumMessage newMessage
object, so if the user creates a new message, or replies to an existing message, I can easily populate the object and send it back to the server.
However, after reading lots of articles on validation, models, etc., it seems to me as though I would be better off using broken-down message fields to be filled out by the user (i.e: String messageComposer; String messageComposer; String messageContents;
), since it will be easier to define validation rules for them.
This is for two reasons:
1) since the forumMessage
object is created using LINQ to SQL, it's kinda of an issue to define its validation rules (yes, I know you can use buddy classes, but I'm kinda scared O_o )
and what seems to be the bigger reason 2) in all validation examples, if the data that was needed to be validated was passed as an object (i.e forumMessage
), then it was the only object in the model. I saw no reference to scenarios in which the model consisted of data which was also used simply to populate the views (in my case - the forum messages for the particular page).
Which brings me to this:
So far, whenever I had to get input from the user, I passed along the objects which I wanted to display or populate as the actual LINQ to SQL objects.
Was I going about this the wrong way? Should I pass LINQ to SQL objects (i.e. classes) for display purposes, but use plain old fields (strings, ints, etc.) for the data I want to retrieve, and create the actual LING to SQL object server side, when I get all the data back to the controller?
I'm at a crossroads here, and would appreciate some help from seasoned veterans :-)
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于它是一个简单的论坛,因此最简单的方法是最好的,即在整个过程中使用 Linq-2-Sql 对象(即从 DAL 到视图)
关于使用对象/属性的混合,我会使用单独的 ViewModel 来聚合数据。
例如,
您的视图(简化的简单版本)
然后您可以在控制器上使用绑定前缀...
As it's a simple forum, then the simplest approach would be best, i.e. using Linq-2-Sql object through the entire process (i.e from DAL to View)
Regarding using a mix of objects/properties, I would use a separate ViewModel to aggregate the data.
e.g.
Your View (cut down, simplistic version)
You can then use a Binding Prefix on your controller ...