对于简化的 Stack Overflow 问题和答案页面,我的模型应该是什么样子
我是第一次尝试 MVC,并尝试构建一个非常简化的 StackOverflow 版本。我有模型/数据库表,例如:
Users
Questions
Answers
Comments
以及这些模型的一些视图模型,我将其用于视图。
模型之间的关系是:
Users 1:m with Questin, Answers, Comments
Questions 1:m with Answers, Comments
Answers 1:m with Comments
和
Users m:m Questions
Users m:m Answers
投票。
对于“问题”页面(与您当前正在阅读的页面相同),我的 ViewModel 应该是什么样子? 该页面应该类似于:
----------------------------------
问题
回答
回答
...
回答
仅用于登录用户回答问题的表单
-----------------------
以及问题和答案下的评论列表
我想出了这个:
public class QuestiongView
{
public QuestionShort question { get; set;}
public IEnumerable<AnswerShort> answers { get; set;}
public AnswerWritabelByUser answerByUser {get; set;} // only for logged in usesr. This is where you type your answer
}
QuestionShort
和AnswerShort
是具有List
的类代码> 在其中。
对于投票,我将使用 $.ajax 调用。
根据我到目前为止所学到的知识,我认为这是正确的方法,但它看起来也很混乱。也许我会使用分部视图来清理视图中的代码。
那么,这是实现 MVC 的正确方法吗?您对改进我针对这个特定问题的整个方法有一些建议吗?我是否知道我在说什么,或者我是否错过了 MVC 的整个设计模式/概念?
I am trying out MVC for the first time and I am trying to build a very simplified version of StackOverflow. I have Models/database tables like:
Users
Questions
Answers
Comments
and some ViewModels of these Models which I use for the Views.
The relations between the models are:
Users 1:m with Questin, Answers, Comments
Questions 1:m with Answers, Comments
Answers 1:m with Comments
and
Users m:m Questions
Users m:m Answers
for votes.
What should my ViewModel look like for the Question page, which is the same as this page that you are currently reading this from?
The page should be something like:
-----------------------
Question
Answer
Answer
...
Answer
form for answering the question only for logged in users
-----------------------
and list of comments under the Question and the answers
I come up with this:
public class QuestiongView
{
public QuestionShort question { get; set;}
public IEnumerable<AnswerShort> answers { get; set;}
public AnswerWritabelByUser answerByUser {get; set;} // only for logged in usesr. This is where you type your answer
}
and QuestionShort
and AnswerShort
are classes which have List<Comments>
in them.
For voting i would use $.ajax calls.
With what i have learned so far i think this is the right way to do it, but it seems messy as well. Maybe i will use partial view to clean up the code in the View.
So, is this the correct way of implementing MVC, do you have some suggestions of improving my whole approach for this particular problem, do I know what am i talking about or have i missed the whole design pattern/ concept of MVC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会做一些非常接近的事情:
模型
控制器
**视图(所有都非常简化)**
Question\Details.cshtml
共享\partial-UserComplex.cshtml
共享\partial-UserSimple.cshtml
共享\partial-Comments.cshtml
Question\partial-Answers.cshtml
优点
缺点
I would do something very close to:
Models
Controller
** Views (All VERY simplified)**
Question\Details.cshtml
Shared\partial-UserComplex.cshtml
Shared\partial-UserSimple.cshtml
Shared\partial-Comments.cshtml
Question\partial-Answers.cshtml
PROS
CONS