0

How to pass View Bag in controller partial view.

Below is my actionResult in partial View:

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")
PrathapG
  • 751
  • 1
  • 7
  • 21
Stfvns
  • 1,001
  • 5
  • 16
  • 42
  • `@Html.Partial()` does not call your server method - it just renders the partial. For that you need to use `@Html.Action()`. But that is an awful way to generate a `` –  Oct 18 '18 at 11:41

1 Answers1

0

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 :

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

iwish from my heart this help you brother

MBARK T3STO
  • 329
  • 1
  • 11