2015-09-07 76 views
0

我需要在從控制器通過視圖傳遞的部分視圖中使用ViewBag/TempData值。 controlller.cs如何在mvc的部分視圖中傳遞和使用ViewBag/TempData值4

var category = (from s in context.M_ProductCategory 
           where s.ID == C_ID 
           select s.Title); 
ViewBag.cat = category; 

Index.cshtml

@{ 
     Html.RenderPartial("PartialViewSpecification", new { ProductCategory = @ViewBag.cat }); 
    } 

我需要使用ViewBag.cat在PartialViewSpecification.Please值幫助

+0

如果你想通過模型(即'IEnumerable ')到部分視圖,那麼它需要是'Html.RenderPartial(「PartialViewSpecification」ViewBag.cat)',部分視圖需要'@model IEnumerable

回答

0

試試這個方法: 控制器:

var category = (from s in context.M_ProductCategory 
           where s.ID == C_ID 
           select s.Title); 
ViewBag.cat = category.FirstOrDefault(); 

Index.cshtml:

@{ 
    Html.RenderPartial("PartialViewSpecification"); 
} 

PartialViewSpecification.cshtml(partialView):

@ViewBag.cat 

我希望這一個工作......

0

Index.cshtml:

@{ 
    Html.RenderPartial("PartialViewSpecification", (string)(ViewBag.cat)); 
} 

PartialViewSpecification.cshtml

@model string 

<span>Model</span> 
相關問題