Asp.net MVC ViewContext (Controller v. actual page)
I have been using the following to render controller specific content:
<% if (ViewContext.Controller is MyNamespace.Controllers.Topic1Controller)
{ %>
[Content specific to Topic 1]
<% } %>
<% else if (ViewContext.Controller is MyNamespace.Controllers.Topic2Controller)
{ %>
[Content specific to Topic 2]
<% } %>
<% else
{ %>
[Default content]
<% } %>
This works great for controller content, since I am able to use one partial view file to host this content and keep it tidy (rather than having to change it different places).
However, now I want to use a partial view on the page (not the controller) level and be able to vary the content (again, to keep things in one file so that I can just edit that file), but I 开发者_Go百科am unable to figure out how I can do if/else if/else statements specific to a page (or the action?).
What I am trying to accomplish is the same as the above code, but specific to pages (Page 1, Page 2, etc.) so that my content changes in the partial view based on the page being viewed.
Any help would be appreciated. I've looked for a bit for the answer, but maybe I'm just not framing my keywords correctly.
Don't reference ViewContext.Controller
. View should be as stupid as possible and definitely should not check controller's type. Why do you wan't to use the same View? If you want to share content, use master pages / nested master pages. All parameters required to render view should be passed in view model class.
public class ViewModel {
int TopicDisplayVersion { get; set; }
...TopicDetails...
}
and then
Html.RenderPartial("Topic", model);
精彩评论