Seshu B

Seshu B

  • NA
  • 442
  • 146.2k

How to display values in listbox from db using ASP.NET MVC

Jun 1 2017 1:57 AM
passing all values in list but it is displaying as NaN in listbox.
 
[AllowAnonymous]
public ActionResult GetColumnnames(ReportsModel Model)
{
string Tablename = Request.Form["Tables"].ToString();
List<string> ColumnNamesList = DBFunctionOBj.GetColumnNames(Tablename);
SelectList Values = new SelectList(ColumnNamesList, "", "",0);
Model.Fieldnames = Values;
var result = new { Values };
return Json(result, JsonRequestBehavior.AllowGet);
// return View(Model);
}
 
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>   <script type="text/javascript">     $(document).ready(function () {         $('#Tables').change(function () {             var id = $('#Tables').val();             alert(id);             $.ajax({                 type: "POST",                 url: '@Url.Action("GetColumnnames", "MCPAdmin")',                 data: { Tables: $('#Tables').val() },                 datatype: "json",                 traditional: true,                   success: function (data) {                                        $('#AvailableFields').empty();                     //iterate over the data and append a select option                     $.each(data, function (item) {                         $('#AvailableFields').append('<option value="' + item.Values + '">'                             + + '></option>');                                              })                 }             });         });     }); </script>   
@Html.ListBox("AvailableFields", new SelectList(""), new { @class = "form-control editable" }) 

Answers (2)