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
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.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly