I have a partial view that handles a certain model.
How can I pass it to the partial view from a view that has no references for that model?
My partial view has:
@model MyNS.Models.NewsComment
How can I call from:
@Html.Partial("CommentForm", Model.NewsComments.SingleOrDefault()) //does not work
Assuming that you have no reference it on your view to the model, you can do the following one :
But this is totally against the ASP.NET MVC’s
Seperation of Concernstheory.What I would do is to pass it to the view as model (event if I won’t use it inside my view) and pass it to the Partial from my view. That way you handle the data access from your controller which would be the better approach.
UPDATE
I have just seen you comment about the error. On this case what you would do is as follows for a better approach (the first case I have shown above will also work for this):
Create a ViewModel as follows :
And then pass it your view as follows :
then work on it on your view as follows :