简体   繁体   English

json结果,在视图mvc中结果为“ undefined”

[英]json result,results “undefined” in view mvc

when I pass the following linq result to my dropdown list ,I get "undefined" there,here I have result(in controller),I know the reason could be after "select new" I didn't specified any specific class but I need it to be like this, 当我将下面的linq结果传递给下拉列表时,我在那里得到“未定义”,这里有结果(在控制器中),我知道原因可能是在“选择新”之后,我没有指定任何特定的类,但是我需要就像这样

    var newlist_of_device_types = (from z in DB.t_device_details
                                           where z.type_id == DB.t_device_type.Where(t => t.device_type == "PLC")
                                          .Select(t => t.type_id)
                                          .FirstOrDefault()
                                           select new
                                           {

                                           model = (z.model + "-" + z.producer),
                                          }).Where(z => z.model != null).ToList();

" View " 查看

  $.ajax({
                        dataType: "json",
                        type: "POST",
                        url: "@Url.Action("deviceTypeList","AdminTool")",
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify({ "flg": flg_dvType, }),
                        success: function (data) {
                            mydata_deviceType = data;
                        }

                    });

The screen shot of what I get in Ajax 我在Ajax中得到的屏幕截图

屏幕截图

you need to return json from controller. 您需要从控制器返回json。 I see you are using POST (make sure if u use get then you need to allow get - JsonRequestBehavior) So do like this: 我看到您正在使用POST(请确保如果您使用get,那么您需要允许get-JsonRequestBehavior)这样做:

    var newlist_of_device_types = (from z in DB.t_device_details
                                       where z.type_id == DB.t_device_type.Where(t => t.device_type == "PLC")
                                      .Select(t => t.type_id)
                                      .FirstOrDefault()
                                       select new
                                       {

                                       model = (z.model + "-" + z.producer),
                                      }).Where(z => z.model != null).ToList();
    return Json(newlist_of_device_types);

And your ajax is actually correct: 而且您的ajax实际上是正确的:

    $.ajax({
        url: "@Url.Action("deviceTypeList","AdminTool")",
        type: "POST",
        data: JSON.stringify(jsonObject),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (response) {
            alert(response.responseText);
    },
        success: function (response) {
            var $select = $('#ID of your dropdown list');
      $.each(response, function(i, m) {
     console.log(m);
     var option = $('<option>', {
     value: m.model //since you have no id here
   }).html(m.model).appendTo($select);
        console.log(option);
      });

        }
    });

Hope this helps!! 希望这可以帮助!!

Maybe you can post a screenshot of your debugging console in browser so i can see the data 也许您可以在浏览器中发布调试控制台的屏幕截图,以便我可以看到数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM