views:

56

answers:

1

I am using MVC2 in VS2010 with .Net 4.0. I have a situation in which I will have a bunch of partial views that I need to all have the same template. Basically I need them all wrapped with the same DIV (and other junk). I just want one place to change this DIV... so I used a master page. It works great, all the partial views use that master, and if I need to change something its no problem.

Problem space: I need to have some of the View specific data, in this case its the Id of the record the view displays, shown in the Id id the DIV. Now I realize I could just create another placeholder, but I need this id to be in 10 places, not 1.

<div id="<%: ViewObjectId %>">
  <asp:contentplaceholder id="ViewStuff" runat="server"/>
</div>

I have tried to add the Id to the ViewData, however it seems in MVC2 the ViewData is missing in action.

I could create a parent Action that calls a child view, but the problem with that is it takes away from the flexibility of having any parameters I want... all views would have to use the same signature.

Any thoughts or suggestions?

A: 

Not sure what you mean by "however it seems in MVC2 the ViewData is missing in action." ViewData is there and should work for what you want. You'll have to access it as ViewData["ViewObjectId"] if you want it, though.

If this doesn't work, could you post code samples to show how you are trying to set the ViewData value?

Ryan Riley
Well, if I put in this code to the master page, I get the below error:<label id="<%: (Guid)ViewData["Id"] %> "></label>Error 3 The name 'ViewData' does not exist in the current context
CrazyDart
Ok, nevermind. The mystery is solved. When I created a new master page, it created a Web Forms master not an MVC one... which still worked. It did not however inherit System.Web.Mvc.ViewMasterPage, rather its own codebehind. So I changed the top tag and deleted the codebehind and its all better. :-)
CrazyDart
Congrats! I thought about suggesting that, but then figured I would wait to see your code. It's an easy mistake to make. Glad you got it sorted. :)
Ryan Riley