2014-11-21 87 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或者在你的模型中。

相關問題