简体   繁体   English

MVC3.0 + c#+ razor:如何创建一个显示帖子和评论创建部分的页面

[英]MVC3.0+c#+razor: How to create ONE page where posts are shown and comment create part

I'm trying to create a weblog with mvc. 我正在尝试使用mvc创建一个Weblog。 I made a database code first with EF. 我首先使用EF编写了数据库代码。 Now I have a page where you can see one post per page. 现在我有一个页面,您可以在每页看到一个帖子。 Below I want to show all comments on the post. 下面,我想显示该帖子的所有评论。 Thats all working fine. 一切都很好。 But now I want to make a create comment functionality on that same page. 但是现在我想在同一页面上创建创建评论功能。

I'm not sure how to do this? 我不确定该怎么做? Because this has to create a new object 'comment' instead of the 'post' object I've set as Model in my view. 因为这必须创建一个新的对象“ comment”,而不是我在视图中设置为“模型”的“ post”对象。 So I've got different models in my view? 所以我认为有不同的模型吗? I don't think that's possible right? 我认为不可能吗? Maybe I can just use the 'post' model because it has comments as a list<> in it? 也许我可以仅使用“发布”模型,因为其中包含注释作为列表<>?

Do I need to use partial views for this or maybe a model view? 我需要为此使用局部视图还是模型视图?

Hope you know what I mean and what I'm trying to accomplish. 希望您知道我的意思以及我要完成的工作。 Tnx in advance for any help! 提前Tnx寻求帮助!

If your Post model contains a List<Comment> then you could of course use that. 如果您的Post模型包含List<Comment>那么您当然可以使用它。 Even if you do go that route, using a ViewModel to wrap all of your model objects is never a bad idea. 即使您走了那条路,使用ViewModel包装所有模型对象也绝不是一个坏主意。

Simply for the sake of maintainability, I would use partial views for the different models rendered on your page, but that's is purely a matter of personal preference. 只是出于可维护性的考虑,我将对页面上呈现的不同模型使用局部视图,但这纯粹是个人喜好问题。

Your post should have a comment collection of some sort. 您的帖子应该有某种评论集。 You should be able to just add a non-model-bound form; 您应该能够只添加一个非模型绑定的表单; in your controller, you have your collection , just pull the data out of that. 在控制器中,您有collection ,只需从中提取数据即可。

What I mean is: in your view, you'll have something like 我的意思是:在您看来,您将拥有类似

@Html.TextArea("CommentText")

In your controller: 在您的控制器中:

public ActionResult Create(FormCollection collection) {
  string commentText = collection["CommentText"];
  Post p = ... ; // Not familiar with EF
  p.Comments.Add(new Comment(commentText));
  p.Save(); // ActiveRecord style, not sure about EF
}

It should be pretty straight-forward. 它应该很简单。 I use ActiveRecord on NHibernate, so I'm not sure about Entity Framework specifically. 我在NHibernate上使用ActiveRecord,所以我不确定具体关于Entity Framework。 Was there some specific problem you got stuck on? 您是否遇到某些特定问题?

Yes you can do it by using a partial view. 是的,您可以使用局部视图来实现。 Make a partial view that posts comments to server. 进行部分视图以将评论发布到服务器。 Display this partial view below comments list. 在评论列表下方显示此局部视图。 When user post the comment then submit it via json call and on success make html string of comment and append it in the comments list. 当用户发布评论时,然后通过json调用提交评论,成功后,将html字符串评论添加到评论列表中。 In this way your comment will be posted as well as loaded in comments list without reloading the whole model 这样,您的评论将被发布并加载到评论列表中,而无需重新加载整个模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM