从 Controller 获取 JSON 数据到 View 中的 Dropdown

我是 MVC 的新手,不知道如何解决这个问题。


在控制器中,我有一个序列化为 JSON 的列表(填充有 Api 数据),我需要使用此 JSON 数据来填充视图中的下拉列表。


我很困惑我应该从控制器返回什么,接下来我应该做什么,我这样做对吗?


现在我有这个:


模型:


public class Tablet {        

    public int Id { get; set; }

    public string ManufactureDate { get; set; }

    public string Description { get; set; }

    public string Country { get; set; }

}

数据控制器.cs


 Public ActionResult GetData(Tablet tablet)

 {

     List<Tablet> data = new List<Tablet>();


      // ... Code to retrieve the data from API


     string jsonRes = JsonConvert.SerializeObject(data);


     return View(jsonRes);

 }

在视图中,我需要在下拉列表中显示 ID:


查看.cshtml


<select class="dropdown" id="dropdownData"></select>


<script>

$(document).ready(function () {

    $.ajax({

        url: "/Data/GetData/",

        type: 'GET',

        success: function (jsonRes) {

            console.log(jsonRes[i]);

            var s = '<option value="-1">Please Select</option>';

            for (var i = 0; i < jsonRes.length; i++) {

                s += '<option value="' + jsonRes[i].Id+ '">' + '</option>';

            }

            $("#dropdownData").html(s);

        }

    });

});  

</script>


慕妹3242003
浏览 105回答 3
3回答

婷婷同学_

试试这个,你正在设置值,你没有为选项标签设置文本,你必须得到空白菜单项。已经使用你的数据链接和代码对其进行了测试。s += '<option value="' + jsonRes[i].Id+ '">'+jsonRes[i].Id + '</option>';完整的 HTML:<!DOCTYPE html><html><head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script></head><body><select class="dropdown" id="dropdownData"></select><script>$(document).ready(function () {&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; url: "https://api.myjson.com/bins/6jd1s",&nbsp; &nbsp; &nbsp; &nbsp; type: 'GET',&nbsp; &nbsp; &nbsp; &nbsp; success: function (jsonRes) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(jsonRes[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var s = '<option value="-1">Please Select</option>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < jsonRes.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s += '<option value="' + jsonRes[i].Id+ '">'+jsonRes[i].Id + '</option>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$("#dropdownData").html(s);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });});&nbsp;&nbsp;</script></body></html>

郎朗坤

删除行 string jsonRes = JsonConvert.SerializeObject(data);您的方法 GetdData() 也应该返回 JSON。检查以下代码。public ActionResult GetData(Tablet tablet)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; List<Tablet> data = new List<Tablet>();&nbsp; &nbsp; &nbsp; &nbsp; data.Add(new Tablet() { Id = 1, Country = "India", Description = "Test", ManufactureDate = DateTime.UtcNow.ToShortDateString() });&nbsp; &nbsp; &nbsp; &nbsp; data.Add(new Tablet() { Id = 1, Country = "Canada", Description = "Test1", ManufactureDate = DateTime.UtcNow.ToShortDateString() });&nbsp; &nbsp; &nbsp; &nbsp; //string jsonRes = JsonConvert.SerializeObject(data);&nbsp; &nbsp; &nbsp; &nbsp; return Json(data,JsonRequestBehavior.AllowGet);&nbsp; &nbsp; }视图应该像<select class="dropdown" id="dropdownData"></select><script>$(document).ready(function () {&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; url: "/Home/GetData/",&nbsp; &nbsp; &nbsp; &nbsp; type: 'GET',&nbsp; &nbsp; &nbsp; &nbsp; dataType: "json",&nbsp; &nbsp; &nbsp; &nbsp; success: function (jsonRes) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(jsonRes[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var s = '<option value="-1">Please Select</option>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < jsonRes.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s += '<option value="' + jsonRes[i].Id + '">' + jsonRes[i].Id+ '</option>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#dropdownData").html(s);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });});</script>

慕哥6287543

尝试这个:数据控制器:[HttpGet]public JsonResult GetData(){&nbsp; &nbsp; List<Tablet> data = new List<Tablet>();&nbsp; &nbsp; // ... Code to retrieve the data from your API&nbsp; &nbsp; string jsonRes = JsonConvert.SerializeObject(data);&nbsp; &nbsp; return new JsonResult(jsonRes);}在你的 JavaScript 中:&nbsp;$.ajax({&nbsp; &nbsp; &nbsp; &nbsp; url: "/Data/GetData/",&nbsp; &nbsp; &nbsp; &nbsp; type: "GET",&nbsp; &nbsp; &nbsp; &nbsp; dataType: "json",&nbsp; &nbsp; &nbsp; &nbsp; cache: false,&nbsp; &nbsp; &nbsp; &nbsp; success: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var parsedData = JSON.parse(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var $select = $('#dropdownData');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.each(parsedData, function(i, dataItem) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('<option>', {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value: dataItem.Id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).html(dataItem.Id).appendTo($select);&nbsp; // or dataItem.Description, or whatever field you want to display to the user&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (err) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(err);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; error: function (e) {&nbsp; &nbsp; &nbsp; &nbsp; console.log(e);&nbsp; &nbsp; }});
打开App,查看更多内容
随时随地看视频慕课网APP