0

my ajax code is :

  $.ajax({
            url: "/Dashboard/filter",
            data: jsonData,
            traditional: true,
            contentType: 'application/json',
            dataType: 'json',
            type: "POST",
            success: function (data) {
                console.error(data);

            },
            error: function (error) {
                console.error(error);
            }
        });

and my controller action :

    [HttpPost]
    public JsonResult filter(Utility.FJson filterjson)
    {

        return Json(new { });
    }

in Utility class:

 public class FilterJson
    {
        public string IdField { get; set; }
        public string NameField { get; set; }
        public int Check { get; set; }
        public string SFilter { get; set; }
        public string TFilter { get; set; }
    }
    public class FJson
    {
        public List<FilterJson> filter { get; set; }

    }

error massage is :

POST http://localhost:38064/Dashboard/filter 500 (Internal Server Error)

i want send json to mvc controller. if JSON.stringify(jsonData) in send time .in controller filterjson variable is NULL

4
  • What does you Json you create in you JavaScript look like? Commented Jun 16, 2018 at 13:56
  • 500 error means your server code is crashing.Put a breakpoint in your action metod code and debug. Also you can see the ajax call in your devtools->network tab. Check the response tab on that call. That might give you some more information about the exception Commented Jun 16, 2018 at 16:37
  • Also, what is inside jsonData ? Commented Jun 16, 2018 at 16:44
  • @amiryami Pl, provide the definition of C# class Utility.FJson and also value of js object jsonData Commented Jun 16, 2018 at 17:17

1 Answer 1

1

You can use the following code to send and receive Json in the form of a class

Ajax code is:

 var data = "{filter :[{IdField: '2', NameField: 'filter item', Check: 1 , SFilter: 'filter search' , TFilter: 'filter text'}]}";
    $.ajax({
        url: '/home/filter',
        dataType: 'json',
        type: 'POST',
        data: data,
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            alert(data);
        },
        error: function () {
            alert("error");
        }
    });

Class:

public class FilterJson
{
    public string IdField { get; set; }
    public string NameField { get; set; }
    public int Check { get; set; }
    public string SFilter { get; set; }
    public string TFilter { get; set; }
}
public class FJson
{
    public List<FilterJson> filter { get; set; }

}

Controller action is:

[HttpPost]
public JsonResult filter(FJson filterjson)
{

    return Json(new { });
}
Sign up to request clarification or add additional context in comments.

Comments

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.