Search code examples
c#asp.net-mvcrazorviewbag

Put ViewBag Controller Value on View


I'm trying to pass a ViewBag data from my controller to my View, if i put the Viewbag on a Html.DropDownList I get the correct values but if I use <input value="@ViewBag.Group" /> I only get "System.Web.Mvc.SelectList" Why am I not getting the value of my viewbag?

This is my controller ViewBag:

    public ActionResult Index(string Selected)
    {
       List<string> listadesumas = new List<string>();
        var db = new PosContext();

      foreach (var item in db.Pos)
        {
            listadesumas.Add(item.ToString());

        }
        var grupos = new SelectList(listadesumas.ToList());

        ViewBag.Group = grupos;
 return View("~/Views/HomePos/Index.cshtml",db.Pos.ToList());
    }

and my Input From View:

  <input value="@ViewBag.Group" />

what am i doing wrong?


Solution

  • @{
       var group = (SelectList)ViewBag.Group
     }
    

    <select> @foreach(var item in group) { <option value='@item'>@item</option>
    } </select>