Why isn't viewbag values displaying in browser even it return values?

enter image description hereList of items passing from an action method to another action in my controller and the values stored into the View Bag. Even it shows values while debugging. It isn't displaying on the browser. I want to put these values in a select box. How could i solve this? somebody please help me.

** in Controller**

public ActionResult Index()
{
    ViewBag.s = TempData["plist"];
    return View("Index"); 
}


[HttpPost]
     public ActionResult  matchpin(string id)
        {
            int d = Convert.ToInt32(id);
            List<pin> plist = new List<pin>();
            using (SqlConnection con = new SqlConnection(constring))
            {
                using (SqlCommand cmd = new SqlCommand("getpin", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.AddWithValue("@pin", d);
                    con.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        var pin = new pin()
                        {


                            po = Convert.ToString(reader["po"]),

                        };
                        plist.Add(pin);
                    }


                }
            }

            TempData["plist"] = plist;
            return RedirectToAction("Index", "home");

        } 

**In view:

<select name="postoffice">                   
    <option>-select-</option>
    @if (ViewBag.s != null)
    {
        foreach (var f in ViewBag.s)
        {
            <option value="@f.po">@f.po</option>
        }
    } 
</select>

1 answer

  • answered 2019-05-22 06:18 Pr0mis PAtel

    You can view the data in select list like that

    In Controller

     public ActionResult Index()
        {
            string firstnamevalue = "Hello";
            string lastnamevalue = "Welcome";
    
            List<string> list = new List<string>();
            list.Add(firstnamevalue);
            list.Add(lastnamevalue);
    
            TempData["plist"] = list;
            ViewBag.s = TempData["plist"];
            return View("Index");
        }
    

    In View

    <select name="postoffice">
    <option>-select-</option>
    @if (ViewBag.s != null)
    {
        foreach (var f in ViewBag.s)
        {
    
            <option value="@f">@f</option>
        }
    }
    

    Output enter image description here When you can not satisfy this answer, please edit your question in the controller. Then tell me how do you pass data?