2015-09-07 77 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> 
相关问题