我刚刚正确地进入 MVC,我被困在一个我认为相对简单的概念上。
所以,我有一个表格,其中发布到控制器(这很好),此时我有:
控制器
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);
}
看法
@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?!
现在,我想要的是当您单击“更多详细信息”(ActionLink)时,将显示产品详细信息,它们是单个 Product 对象的一部分。我从 GET 中调用了 TestAction 控制器,但是如何保留产品视图并显示单个产品的详细信息?将这个单一的产品分配给 ViewBag 并这样做?那么,对于产品列表,缓存原始列表并使用该缓存?
我希望通过回发来完成此操作,因为这是针对我网站的非 JS 版本的。
当然必须有更好的方法来做到这一点,或者我是不是已经被 ViewState 养得太久了?