简体   繁体   English

在控制器局部视图中访问ViewBag

[英]Access ViewBag in Controller Partial View

How to pass View Bag in controller partial view. 如何在控制器局部视图中传递View Bag。

Below is my actionResult in partial View: 以下是我在局部视图中的actionResult:

public ActionResult _Container()
{
   var ListContainerType = db.tblM_ContainerType.Where(a => a.IsActive == true).Select(a => new { a.Id, text = a.Code + " - " + a.Name }).ToList();
   List<SelectListItem> slContainerType = new SelectList(ListContainerType, "Id", "text").ToList();
   ViewBag.ContainerTypeId = slContainerType;

   List<SelectListItem> slContainerSize = new SelectList(db.tblM_ContainerSize.Where(a => a.IsActive == true), "Id", "Detail").ToList();
   ViewBag.ContainerSizeId = slContainerSize;

   return View();
}

When access in partial view have error called it: 在部分视图中访问时,有一个错误调用:

<select type="text" name="ddlTypeContainer" id="ddlTypeContainer" class="form-control" required>
    <option value="">&nbsp;</option>
    @foreach (var item in ViewBag.ContainerTypeId)
    {
        <option value="@item.Value">@item.Text</option>
    }
</select>

How I call my partial View like this: 我如何这样称呼我的局部视图:

 @Html.Partial("_Container")

if you mean how to call partial view from any view i suggest you this simple solution 如果您的意思是如何从任何视图调用局部视图,我建议您这种简单的解决方案

firstly your partial view should be founded or placed in the Shared Folder in your project if in your project doesn't have Shared folder you can easy to create them just in the Views Folder create new folder and name it Shared and copy past your partial view ( your .Cshtml file ) and call them using : 首先,如果您的项目中没有共享文件夹,则可以在视图文件夹中轻松创建它们,然后将其命名为Shared并复制到您的部分视图中,这样就可以在您的项目的“共享文件夹”中建立或放置部分视图(您的.Cshtml文件),然后使用以下命令调用它们:

@RenderPage("~/Views/Shared/_Container.cshtml");

iwish from my heart this help you brother 真心希望,这对你兄弟有帮助

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

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