[英]Asp.Net Mvc Passing Parameters Between Partial Views
I have main view as below : 我的主要观点如下:
<div class="wellContainer" id="stockCardGroupContainer">
@Html.Action("_StockGroupMenu", new { stockGroupId = 1})
</div>
<div id="stockCardContainer" class="wellContainer">
@Html.Action("_Index", "WrhStockCard", new { stockGroupId = ViewBag.wrhRelatedStockGroupId })
</div>
Here is the methods which returns partial views. 这是返回部分视图的方法。
public PartialViewResult _StockGroupMenu(int? id)
{
var wrhStockGroup = db.WrhStockGroup.Include(w => w.RelatedWrhStockGroup).Where(p => p.RelatedWrhStockGroupId == id);
ViewBag.wrhRelatedStockGroupId = id;
ViewBag.stockGroupName = db.WrhStockGroup.Find(id).Name;
return PartialView(wrhStockGroup.ToList());
}
public PartialViewResult _Index(int? stockGroupId)
{
var relatedStockCards = stockCardService.GetStockCardsByGroupId(stockGroupId);
ViewBag.stockGroupId = stockGroupId;
ViewBag.stockGroupName = db.WrhStockGroup.Find(stockGroupId).Name;
return PartialView(relatedStockCards);
}
Scenario is, when user click a group, i need to show stock cards "stockCardContainer" and also show, related childGroupd in stockCardGroupContainer . 场景是,当用户单击一个组时,我需要显示股票卡“ stockCardContainer”,并在stockCardGroupContainer中显示相关的childGroupd。 But i set ViewBag.wrhRelatedStockGroupId in _StockGroupMenu method but cant pass it to second @Html.Action since it is different partial view.
但是我在_StockGroupMenu方法中设置了ViewBag.wrhRelatedStockGroupId,但是由于它是不同的局部视图,因此无法将其传递给第二个@ Html.Action。
There are multiple ways of doing this. 有多种方法可以做到这一点。 Try using
TempData
- it will keep the data for the next request, and it gets cleared automatically. 尝试使用
TempData
它会保留下一个请求的数据,并且会自动清除它。 So in your controller actions, for the data you need to pass into another action/partial view, replace lines with ViewBag
assignments to something like this: 因此,在您的控制器动作中,对于需要传递到另一个动作/局部视图的数据,请使用
ViewBag
分配替换行:
TempData["wrhRelatedStockGroupId"] = id;
Then read it in your next action and put it in a ViewBag
or in your model. 然后在下一个动作中阅读它,并将其放入
ViewBag
或模型中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.