ViewBag data from controller is not visible in Partial View


I am passing Select List from ViewBag in controller to partial View, like I used to do , but it is not showing SelectList on partial View when code is pushed on server, while it works when running code locally, here is the code

Controller

        public ActionResult GetAddressOtherDetails(int id, int AddId)
        {
              AddressProviderDetail apd = new AddressProviderDetail();
            using (var context = new bwavenueEntities())
            {
              // some code removed for brevity

                    var Item = new List<ClassForConnectWise.DropdownListForCompany>();
                    JavaScriptSerializer oJS = new JavaScriptSerializer();
                    var oRootObject = oJS.Deserialize<List<ClassForConnectWise.Class1>>(response.Content);
                    if (oRootObject != null)
                    {
                        foreach (var data in oRootObject)
                        {
                            Item.Add(new ClassForConnectWise.DropdownListForCompany { ID = data.id.ToString(), Value = data.name });
                        }
                    }
                  //Select List item in ViewBag
                    ViewBag.DropdownListSelect = new SelectList(Item, "ID", "Value");
                   

              
               
           }
           //apd is Model
            return PartialView(apd);
       }

Partial View Code(Razor)

@Html.DropDownList("DropdownListSelect", ViewBag.DropdownListSelect as List<SelectListItem>);
        

Now, i am not sure, what is the issue in above code, because of which it is not rendering DDL on server? I can change approach, if needed


Asked by:- Sam
0
: 8891 At:- 12/6/2017 12:39:45 PM
C# asp.net-mvc razor mvc-partial-view ViewBag







1 Answers
profileImage Answered by:- pika

I am not sure, why this is not working it is the correct code and you can pass ViewBag to PartialView with any type of value, check if you are getting data in oRootObject on server at all or not?

If that is still not working try to make dropdown list in C# Controller and pass string on the front end using ViewBag and then render it using @Html.Raw

So, your C# code would be like this

public ActionResult GetAddressOtherDetails(int id, int AddId)
        {
              AddressProviderDetail apd = new AddressProviderDetail();
            using (var context = new bwavenueEntities())
            {
              // some code removed for brevity

                    var Item = new List<ClassForConnectWise.DropdownListForCompany>();
 var str = "<select id='DropdownListSelect' name='DropdownListSelect' style='width:200px'>";
                    JavaScriptSerializer oJS = new JavaScriptSerializer();
                    var oRootObject = oJS.Deserialize<List<ClassForConnectWise.Class1>>(response.Content);
                    if (oRootObject != null)
                    {
                        foreach (var data in oRootObject)
                        {
                            Item.Add(new ClassForConnectWise.DropdownListForCompany { ID = data.id.ToString(), Value = data.name });
 str = str + "<option value='" + data.id.ToString() + "'>" + data.name + "</option>";
                        }
                    }
              str = str + "</select>";
                  //Select List item in ViewBag
                    ViewBag.DropdownListSelect = str;                              
           }
           //apd is Model
            return PartialView(apd);
       }

In View Render it as

@Html.Raw(ViewBag.DropdownListSelect);

This will work for sure, as now ViewBag is string and you can pass it to View in PartialView.

2
At:- 12/7/2017 2:39:33 PM Updated at:- 12/7/2017 2:39:43 PM
thanks, my issue was i getting empty dropdown :) 0
By : Sam - at :- 12/12/2017 2:45:28 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use