Arturo Martinez
Arturo Martinez

Reputation: 385

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?

Upvotes: 0

Views: 1532

Answers (3)

Arturo Martinez
Arturo Martinez

Reputation: 385

i manage to get another aproach using a boxlist, heres the code in case someone needs it:

   @Html.ListBox("Group", (IEnumerable<SelectListItem>)ViewBag.Group)

Upvotes: 0

RiceRiceBaby
RiceRiceBaby

Reputation: 1596

Viewbag.Group contains a list of values. An input tag can only take exactly one value.

Upvotes: 0

ManojAnavatti
ManojAnavatti

Reputation: 644

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

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

Upvotes: 1

Related Questions