简体   繁体   English

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

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

I need to use ViewBag/TempData value in partial view which is passed from controller through view. 我需要在部分视图中使用ViewBag / TempData值,该值从控制器通过视图传递。 controlller.cs controlller.cs

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

Index.cshtml Index.cshtml

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

I need to use the value of ViewBag.cat in PartialViewSpecification.Please help 我需要在PartialViewSpecification中使用ViewBag.cat的值。请帮助

Try this way: Controller: 尝试这种方式:控制器:

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

Index.cshtml: Index.cshtml:

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

PartialViewSpecification.cshtml(partialView): PartialViewSpecification.cshtml(partialView):

@ViewBag.cat

I hope this one is working... 我希望这是可行的...

Index.cshtml: Index.cshtml:

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

PartialViewSpecification.cshtml : PartialViewSpecification.cshtml

@model string

<span>Model</span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM