如何在 mvc 4 的局部视图中传递和使用 ViewBag/TempData 值

How to pass and use ViewBag/TempData value in partial view of mvc 4

我需要在从控制器通过视图传递的部分视图中使用 ViewBag/TempData 值。 controlller.cs

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

我需要使用 PartialViewSpecification.Please 帮助中 ViewBag.cat 的值

试试这个方法: 控制器:

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(部分视图):

@ViewBag.cat

我希望这个有用...

Index.cshtml:

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

PartialViewSpecification.cshtml:

@model string

<span>Model</span>