views:

122

answers:

2

Hi,

I am creating a very simple forum as my first MVC project. My database layout is rather simple:

[ForumThread]
Id
Title

[ForumPost]
Id
ThreadId
Message
ParentId // To tell which post this post should hang on to
Created
CreatedBy
Updated
UpdatedBy

I am creating a view for the ForumThread for displaying the list of threads and to be able to create a new thread.

There is a details view of the ForumThread which shows the thread with the underlying posts.

My question is how I the easiest way in the ForumThread details view, can display a view to create a ForumPost, without having to navigate to another page first?

+1  A: 

You could create a partial view (.ascx) which will contain the form allowing you to create a forum post and include this partial in the details view:

<% Html.RenderPartial("~/Views/Home/PostForm.ascx"); %>
Darin Dimitrov
Thanks, but when I tried to do that I get the following error: The model item passed into the dictionary is of type 'SampleWebsite.Models.Forum.ForumThreadModel', but this dictionary requires a model item of type 'SampleWebsite.ForumPost'.
Dofs
You could pass a different model to the partial: `<% Html.RenderPartial("~/Views/Home/PostForm.ascx", Model.ForumPost); %>` which should be of the correct type.
Darin Dimitrov
+1  A: 

As mentioned by Darin, you simply need a partial view - within that view you can implement the form, bind to a different model etc and also handle any events etc by a different controller if needs be.

It's also nice to encapsulate areas of functionality into partial views - keeps the code looking clean and if you find yourself needing to use that form again, then it follows the DRY (Don't Repeat Yourself) principle - you just add another Html.RenderPartial() call into wherever you need it.

Michael