开发者

Edit one model on the view page of another - ASP.NET MVC

I am writing a basic blogging application in ASP.NET MVC. I have a Post model which can be displayed via View.aspx. On that page I also want to have a form to submit a com开发者_JAVA技巧ment, but as View.aspx inherits the Post model, I don't know how to have it edit a Comment model.


In your controller you can use whatever model you like. So place a form in your view which points to Post/Comment/{postid} or Comment/Add/{postid}. After you added the model to the database redirect to the previous view.


tsv - create a few partialviews for the comments (strongly typed) and create a couple of methods on the comment controller:

  • GetComments(int postId)
  • AddComment(Comment newComment)

in your post view, call the partialview either via $ajax or directly in the page.

jim


The key thing to understand here is when your page 'inherits' a class (Post, in your case), this is purely so that the Model does not need to be cast in any way. This does not restrict you from referring to other classes within the aspx page, nor does it prevent the page from having a form which posts to another Controller/Action that inherits from a completely different class (Comment, in your case).

Maybe if you posted some example code, we could suggest ways to make it work the way that you want.


As Jim said, you can create the necessary methods in a different or in the same Controller and call <% Html.RenderAction("methodName","ControllerName") %> in your view.aspx which will still inherit the Post Model. Then, create usercontrols for the form to submit a comment and to display all the comments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜