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

时间:2015-09-07 09:50:10

标签: asp.net-mvc razor asp.net-mvc-partialview

我需要在部分视图中使用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中使用ViewBag.cat的值。请帮助

2 个答案:

答案 0 :(得分: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

我希望这个有效......

答案 1 :(得分:0)

<强> Index.cshtml:

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

<强> PartialViewSpecification.cshtml

@model string

<span>Model</span>