0

im new in MVC,i want to bind my dropdown list when the page is loaded,for that I want to send my controller a flag to send me back the list I want for my drop down list,but the problem is when page loads,it does not send the flag,directly goes to controller

my view:

  $(document).ready(function () {
          var flg_parkList = "s";

          debugger;

          $.ajax({

              dataType: "json",
              type: "POST",
              url: "@Url.Action("parkList","Ranking")",
              contentType: "application/json; charset=utf-8",
              data: JSON.stringify({ "flg": flg_parkList }),
              success: function (data) {
                  debugger;


              }

          });
          $('#drplist')
          .appendTo(container)
          .kendoDropDownList({
              dataSource: data,
              dataTextField: "Text",
              dataValueField: "Text",
              valuePrimitive: true,

          });

      });

My controller:

 public JsonResult parkList(string flg)
    {
        string useID = HttpContext.User.Identity.Name;
        if (flg == "s")
        {

            var listOfParks = (from s in DB.MasterDatas
                               join m in DB.UsersTurbines
                               on s.turbine_id equals m.tur_id
                               where m.user_id == useID
                               select new SelectListItem
                               {
                                   Text = s.turbine_windpark_name
                               }).ToList().Distinct();

            return Json(listOfParks, JsonRequestBehavior.AllowGet);
        }
        else
            return null;

    }
  • 2
    You binding your drop down list to `data` before the ajax call has completed (ajax is async). You need to bind it in the success callback –  Oct 05 '17 at 07:34
  • This question is not clear. You know you don't have to `Stringfy` your object in `data`? `data` can happily accept an object. – Liam Oct 05 '17 at 07:35
  • `$('#drplist').appendTo(container)` placed outside `success` callback, that's why the DDL doesn't bind. Also seems that `JSON.stringify` is unnecessary there... – Tetsuya Yamamoto Oct 05 '17 at 07:36
  • @StephenMuecke yes but why in my controller the flg is empty?i initialized my flg in ajax and sent to controller! –  Oct 05 '17 at 07:44
  • It will not be empty based on the code you have shown - it will have a value of `"s"` –  Oct 05 '17 at 07:47
  • @bravo83, do you have any errors ? – Mihai Alexandru-Ionut Oct 05 '17 at 07:52
  • please look at developer tool OR put the debugger point on controller .. are you sure , are you hitting your controller via ajax ??? – Asif Raza Oct 05 '17 at 10:59
  • @AsifRaza I want when the page loads,my ajax trigger,but does not go to ajax –  Oct 05 '17 at 11:33

1 Answers1

0

I think you don't need ajax. Look at this piece of code

http://www.compilemode.com/2016/01/bind-dropdownlist-using-viewbag-in-asp-net-mvc.html

You can solve your problem with ViewBag. But If you want to use ajax , you can search like this on google ==> "How to fill dropdown list with ajax in Asp.net MVC". Lots of example are availble :D Have a nice coding, happy programming :D

Eyup Can ARSLAN
  • 692
  • 9
  • 25