views:

79

answers:

3

Let's assume I have 2 Controllers, TopicsController and PostsController.

For each controller, I have a couple of views (Index & Details).

The Topic (Index) view inherits System.Web.Mvc.ViewPage<IEnumerable<MessageBoard.Models.Topic>>

The Topic (Details) view inherits System.Web.Mvc.ViewPage<MessageBoard.Models.TopicFormViewModel>
I'm using a TopicFormViewModel because I'm sending additional data along with the Model.

The Post (Details) view just inherits System.Web.Mvc.ViewPage<MessageBoard.Models.Post>

Now, I've created a partial view (CreatePost.ascx) which is (obviously :p) used to create a new Post. I want to be able to re-use this control on all of the views you see above.

Update
I've tried rendering the partial view using <% Html.RenderPartial("New"); %> from my Topics/Index.aspx View, but that results in an exception

The model item passed into the dictionary is of type 'System.Data.Linq.Table`1[MessageBoard.Models.Topic]', but this dictionary requires a model item of type 'MessageBoard.Models.Post'.

Now the problem is that my partial view (CreatePost.ascx) accepts a System.Web.Mvc.ViewUserControl<MessageBoard.Models.Post> and I'm not sure how to pass that from all my views above.

I'm also unsure know how to submit the .ascx values to a certain URL (i.e. /Topics/1/CreatePost), how do I tell the submit button to post to that URL?

Thanks in advance,
Marko

+1  A: 

Ciao Marko,

Now the problem is that my partial view (CreatePost.ascx) accepts a System.Web.Mvc.ViewUserControl and I'm not sure how to pass that from all my views above.

I am not sure I understand what do you mean by "how to pass that from all my views above" but I am sure that you dont have to pass an instance of Post from your views. What is going on is that from your views you will invoke a controller action that creates the Post model object and then bind it to the CreatePost.ascx partial.

I'm also unsure know how to submit the .ascx values to a certain URL (i.e. /Topics/1/CreatePost), how do I tell the submit button to post to that URL?

You have two options:

Inside your CreatePost.ascx partial you are probably using a form.

<% using (Html.BeginForm("action", "controller", FormMethod.Post, new {} )) { %>

If you use in the way I am showing you can change the first and the second params respectively to the names of the Action and the Controller that would habndle your submit.

The second option is using jQuery. Simply set an ID for your form and then

$("#myForm").submit(function(event) {
    //post an ajax request to the server
});

Hope this helps!

P.S. To be able to reuse your CreatePost.ascx partial place it inside the shared view folder (where your master page is).

Lorenzo
Hi @Lorenzo, Thanks for the tips on `BeginForm`, I didn't know it accepts parameters so I've now fixed that. Cheers! Regarding the other issue, please see my update.
Marko
@Marko: If you do the RenderPartial from inside a view that is strong typed to a MyObject class, the framework will try to pass that instance of object to the RenderPartial method. But your partial is strong typed to another object... You should create an instance of a Post model and then do `<% Html.RenderPartial("CreatePost", myPostInstance); %>`
Lorenzo
Anyway I want to warn you that creating instance of objects inside the views is not the right method...
Lorenzo
A: 

In regards to reusing a partial view which is not in the same view folder, use the following and pass in the model required, alternatively you can define a custom route for it.

<% html.RenderPartial("~/Views/<ControllerName>/<PartialViewName>.ascx", <model>);
Dien
Hey @Dien, my issue is related to passing the model to the RenderPartial, not to finding the Partial View itself :)
Marko