2014-11-21 70 views
0

我有如下的主视图:局部视图之间Asp.Net的mvc传递参数

<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> 

这里是返回部分视图的方法。

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); 
    } 

的情况是,当用户单击一个组,我需要显示股票卡“stockCardContainer”,也显示出,在stockCardGroupContainer相关childGroupd。但我在_StockGroupMenu方法中设置ViewBag.wrhRelatedStockGroupId,但不能将它传递给第二个@ Html.Action,因为它是不同的局部视图。

回答

0

有多种方法可以做到这一点。尝试使用TempData - 它将保留下一个请求的数据,并自动清除。所以,在你的控制器动作,数据需要传递到另一个动作/局部视图,更换ViewBag分配行是这样的:

TempData["wrhRelatedStockGroupId"] = id; 

然后在你的下一个动作阅读并把它放在一个ViewBag或者在你的模型中。