mvc json call back to controller

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
I have the following ajax codes, and want to send Json data to MVC Controller.
Can you show me how to do that  on MVC Controller?
Thanks

function Submit(domain) {
    //alert("Thank You");
    var x = $('#formdiv *').serializeArray();      
    var data = {};
   $.each(x, function (i, field) {
        data[field.name] = field.value;
   });
    var json = JSON.stringify(data);   
    alert(json);
    $(document).ready(function () {
        $.ajax({
            url: domain + "/Api/ReceiveJson",
            type: "POST",
            data: {json},
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function (data) {alert('Success');},
            error: function (msg) { alert(msg); }
        });
    });
    return false;
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
any helps?
leakim971Multitechnician
Top Expert 2014

Commented:
what happen with current code?
how ReceiveJson look currently ?

Author

Commented:
there is nothing in ReceiveJson. I just name it for now.
leakim971Multitechnician
Top Expert 2014

Commented:
Let's try something simple, use this :

[HttpPost]
public JsonResult ReceiveJson(string message)
{
        return "{\"status\":\"you said :" + message + "\"}";
}

Open in new window


with this :

function Submit(domain) {
    //alert("Thank You");
    var x = $('#formdiv *').serializeArray();      
    var data = {};
   $.each(x, function (i, field) {
        data[field.name] = field.value;
   });
    var json = JSON.stringify(data);   
    alert(json);
    $(document).ready(function () {
        $.ajax({
            url: domain + "/Api/ReceiveJson",
            type: "POST",
            data: '{"message":"hello"}',
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function (data) {alert(JSON.stringify(data));},
            error: function (msg) { alert(msg); }
        });
    });
    return false;
}

Open in new window


If you get the message back, you just need to replace "string message" by  "FormModel formmodel", use a class for FormModel and use your the ajax call of your original post

public partial class FormModel
{
    public string formfieldname1 { get; set; }
    public string formfieldname2 { get; set; }
    public string formfieldname3 { get; set; }
    public string formfieldname4 { get; set; }
}

Open in new window

Author

Commented:
[HttpPost]
public JsonResult ReceiveJson(string message)
{
        return "{\"status\":\"you said :" + message + "\"}";
}
create an errror: return can't be string

Author

Commented:
any helps?
Multitechnician
Top Expert 2014
Commented:
public string ReceiveJson(string message)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial