I am displaying an list of Items for a given Order. When a user clicks Add Item I redirect to the Item / Create page. This page collects that necessary input but also needs to know the order id that the item belongs to. What is the appropriate way to pass the OrderID to the Item / Create so that it survives the form post when the newly created item is saved.
I’ve played with TempData and writing the id out on the detail page via Html.Encode(). It gets me part of the way there in that the id shows up on the item form but the value is lost when the form submits and posts. I suppose because its not part of the formcollection. I am guessing my workaround is not the best way and would like to know if anyone can point out the proper way to do this in asp.net mvc.
I do this by creating a new route for the Item controller that includes the OrderId. It doesn’t make sense to have an Item without an Order, so the OrderId is required using the constraints parameter.
So the url would look like
http://<sitename>/Item/Create/8, where 8 is the OrderId for which to create an item. Something similar could be done for Delete action routes withhttp://<sitename>/Item/Delete/8/5, where 8 is the OrderId and 5 is the ItemId.Your Action methods would look like this:
You could also set it up so that the urls looked like
http://<sitename>/Order/8/Item/Createandhttp://<sitename>/Order/8/Item/Delete/5if that seems to more clearly show what’s going on.Then the route would look like this: