views:

97

answers:

3

I have an OrderForm domain class, which has property subclasses, something like:

interface IOrderForm
{
    int OrderId { get; }

    ICustomerDetails CustomerDetails { get; set; }
    IDeliveryDetails DeliveryDetails{ get; set; }
    IPaymentsDetails PaymentsDetails { get; set; }
    IOrderDetails OrderDetails { get; set; } 
}

My "Details" view is strongly typed inheriting from IOrderForm. I then have a strongly type partial for rendering each section:

<div id="CustomerDetails">
    <% Html.RenderPartial("CustomerDetails", Model.CustomerDetails); %>
</div>

<div id="DeliveryDetails">
    <% Html.RenderPartial("DeliveryDetails", Model.DeliveryDetails); %>
</div>

... etc

This works ok up to this point, but I'm trying to add some nice ajax bits for updating some parts of the order form, and I've realised that each of my partial views also needs access to the IOrderForm.OrderId.

Whats the easiest way to give my partials access to this value?

+1  A: 

You would have to change your code a bit but you could do this

<div id="CustomerDetails"> 
    <% Html.RenderPartial("CustomerDetails", new {OrderID = Model.OrderID,  CustomerDetails = Model.CustomerDetails}); %> 
</div> 

<div id="DeliveryDetails"> 
    <% Html.RenderPartial("DeliveryDetails", new {OrderID = Model.OrderID, DeliveryDetails = Model.DeliveryDetails); %> 
</div> 

Or you could assign the OrderID to tempData in the Action

TempData("OrderId") = your orderIDValue
John Hartsock
+1  A: 

There is an overload for RenderPartial that accepts both a Model object and a ViewData dictionary, so potentially you could just set a ViewData value, and pass the ViewData object to the partial.

Another option is to include an Order Id member in your partial ViewModel, and copy the Order Id to it.

Robert Harvey
+2  A: 

you can get the value from your Route using something like

this.ViewContext.RouteData.GetRequiredString["OrderId"]

Or your add it to your viewData and reuse pass it to your partialView

TempData is also a pretty simple way...

moi_meme