none
Single Parameter passed to Core Controller always null

    Pertanyaan

  • I am upgrading mvc application to .Net Core and am having difficulty passing a string value via ajax to the controller. I have tried various solutions I found on the web ([FormBody], prefixing with "=", and some others), but no luck. The value is always null. If I change it to GET, it works.   What has changed in Core that I need to fix?

    Thanks!

           var result = "";
        $.ajax({
            url: "/system/loadchildrenaccounts/",
            type: "POST",
            data: JSON.stringify({ "parentID": "12345" }),
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                result = data;
            },
            done: function (data) {
                result = data;
            },
            fail: function (data) {
                result = data;
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert('request failed :' + errorThrown);
            }
        });
    
    
        

     [HttpPost]
         public string LoadChildrenAccounts(string parentID)
        {
            using (zBestContext db = new zBestContext())
            {
                List<Generic> s = db.Accounts.Where(a => a.ParentAccountID == parentID)
                     .Select(a => new Generic { LabelOrID = a.AccountID, Description = a.Company })
                     .OrderBy(a => a.Description)
                     .ToList();
    
                Generic item = new Generic();
                item.LabelOrID = "";
                item.Description = "*** Company ***";
                s.Insert(0, item);
    
                return s.toJSON();
            }
        }


    Jumat, 23 November 2018 02.28

Semua Balasan

  • You want to Json.stringify a JavaScript object

    https://www.w3schools.com/js/js_json_stringify.asp

    ASP.NET MVC and WebAPI controllers using Ajax can be discussed at the ASP.NET forums and Core too.

    http://forums.asp.net/




    • Diedit oleh DA924x Jumat, 23 November 2018 05.54
    Jumat, 23 November 2018 05.51