Asp.Net Mvc Passing Parameters Between Partial Views

822 Views Asked by At

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 . But i set ViewBag.wrhRelatedStockGroupId in _StockGroupMenu method but cant pass it to second @Html.Action since it is different partial view.

1

There are 1 best solutions below

0
On

There are multiple ways of doing this. Try using TempData - it will keep the data for the next request, and it gets cleared automatically. 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:

TempData["wrhRelatedStockGroupId"] = id;

Then read it in your next action and put it in a ViewBag or in your model.