I’m just getting into MVC properly, and I’m stuck on a concept which I figured would be relatively simple.
So, I have a form, in which posts to a Controller (which is fine), at the minute I have:
Controller
public ActionResult TestAction(int productId)
{
// What to do here... ?
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult TestAction(FormCollection values)
{
// Do work here and return the objects
return View(Products);
}
View
@foreach (var product in Model)
{
<tr>
<td>@product.Provider.Name</td>
<td>£@product.MonthlyPremium</td>
<td>@Html.ActionLink("More Details", "TestAction", new { productId = product.ProductId })</td>
<td>Compare</td>
</tr>
}
// I want the singular product details to be displayed here, under the product list... but how?!
Now, what I want is when you click on “More Details” (the ActionLink) then the product details are going to be displayed, which are part of the singular Product objects. I got the TestAction controller being called from GET, but how do I retain the view of the products and display the details of the singular product? Assign this singular Product to the ViewBag and do it that way? Then, for the list of products, cache the original list and use that cache?
I want this done via postbacks as this is for the non-JS version of my site.
Surely there has to be a better way of doing this, or have I been babied with ViewState for too long?
You could add a property to your model, for example
bool ViewDetail, and set that in your controller for the item that corresponds to theproductIdparameter:And display it on your view:
Or you could alter your model to contain:
Then again, set the
DetailProductbased on theproductIdparameter, and display it in the view if it is not null.