Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi all
i want to display values in listbox from database and values getting in controller json method but displaying as [Object Object] like this

What I have tried:

Controller :-
------------
public ActionResult GetColumnsByFavouriteReport(ReportsModel Model,string Columns)
{
    List<Report> ReportColumnsList = MCPAdminControllerPageObject.GetColumnsByReportName(Columns);
    return Json(ReportColumnsList, JsonRequestBehavior.AllowGet);
}



View :-
-----
 $(document).ready(function () {
      $('#FavouriteReports').change(function () {
          $.ajax({
              type: "POST",
              url: '@Url.Action("GetColumnsByFavouriteReport", "MCPAdmin")',
              data: { Columns: $('#FavouriteReports').val() },
              datatype: "json",
              traditional: true,
              success: function (data) {

                  $('#SelectedFields').empty();
                  $.each(data, function (key, val) {
                      $('#SelectedFields').append('<option id="' + key + '">' + val + '</option>');
                  })

              }
          });
      });

@Html.ListBoxFor(m => m.SelectedFields, new SelectList(new[] { "" }), new { @class = "form-control editable" })  


Model
-----
public List<string> SelectedFields { get; set; }
Posted
Updated 8-Jun-17 20:30pm
Comments
F-ES Sitecore 7-Jun-17 6:59am    
Use debugging (the "debugger" keyword) to examine "data" and "key" and "val". As your json is a list of objects then "val" will likely be an object too with properties so you're going to probably need to use "val.Something" rather than "val" when making your option. We don't know what properties are on "Report" so can't give you a concrete answer. But whatever property is on the object that you want to show in the dropdown is what you need to use, likewise for "key" if relevant.
Karthik_Mahalingam 7-Jun-17 8:25am    
check what value you are getting in console
success: function (data) {
console.log(data)
}
VenkataSeshu B 7-Jun-17 8:42am    
Hi
value is displaying listbox from db now
but displaing as Account,EmailTo,Subject
i need to show like this
Account
EmailTo
Subject

1 solution

Check below solution :

HTML
var items;
$('#SelectedFields').empty();
//var items = '-Select Items-';
$.each(data, function (key, val) {
items += "<option value='" + key + "'>" + val + "</option>";
})
$('#SelectedFields').html(items);
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900