0

Check my code shown below. I am posting some json to a controller method. And I want to receive the values in C# model class called MenuItems; but the problem is: I am able to post data to my controller, but all of my 'MenuItems' return null. Data is not being assigned properly. How can I fix it?

AJAX:

var obj = '[{ "text": "wewer", "target": "_self", "children": [{ "text": "wer", "target": "_top" }] }]';


$.ajax({
        type: "POST",
        url: "/Custom/SaveMenu",
        data: JSON.stringify(obj),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) { alert(data); },
        failure: function (errMsg) {
            alert(errMsg);
        }
    });

Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DemoWeb.ViewModels
{
    public class MenuItems
    {
        public string text { get; set; }
        public string target { get; set; }
        public List<Child> children { get; set; }
    }

    public class Child
    {
        public string text { get; set; }
        public string target { get; set; }
    }
}

Controller:

[HttpPost]
public JsonResult SaveMenu(MenuItems menuItems)
{
    //'menuItems' this returns null for all properties

    //return Json("pokay");
}
3
  • You posting an array, so it needs to be public JsonResult SaveMenu(IEnumerable<MenuItems> menuItems) (note also, you have already stringified it so you don't need to use JSON.stringify() to do it again) Commented Sep 1, 2018 at 4:21
  • Alternatively, keep the existing parameter and remove the outer [ and ] so its an object, not a collection Commented Sep 1, 2018 at 4:23
  • Just apply data: obj, and remove data: JSON.stringify(obj), Commented Sep 1, 2018 at 4:23

1 Answer 1

3

You must remove [ and ] in first and last of obj .

So obj changed from :

var obj = '[{ "text": "wewer", "target": "_self", "children": [{ "text": "wer", "target": "_top" }] }]';

To this:

var obj = '{ "text": "wewer", "target": "_self", "children": [{ "text": "wer", "target": "_top" }] }';

And apply data: obj, and remove data: JSON.stringify(obj),

Full code :

var obj = '{ "text": "wewer", "target": "_self", "children": [{ "text": "wer", "target": "_top" }] }';


$.ajax({
        type: "POST",
        url: "/Custom/SaveMenu",
        data: obj,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) { alert(data); },
        failure: function (errMsg) {
            alert(errMsg);
        }
    });
Sign up to request clarification or add additional context in comments.

2 Comments

obj.slice(1, -1) i did this then passing to controller now but still its null
@johnCogdle I tested my codes . Please replace my codes in your code. did you change data: JSON.stringify(obj) to data: obj ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.