0

I am trying to pass the data of the view model to one js method and from there I need to pass the VM to another controller method to show the data.

here is what I have did:

$(document).ready(function () {

    var content = GetHomeContent('/Home/CastContent');
    if (content) {
        saveBDContent('/Message/Details/', content);
    }

});

function GetHomeContent(url) {
    var modelData;
    $.ajax({
        url: url,
        cache: false,
        async: false,
        type: "GET",
        contentType: 'application/json',
        success: function (data) {
            if (data) {
                modelData = data;
            }
        },
        error: function (data) {
            status = false;
        }
    })

   return modelData;
};

function saveBDContent(url, data) {
    var status = false;

    $.ajax({
        url: url,
        cache: false,
        async: false,
        type: "GET",
        data: JSON.stringify(data),
        contentType: 'application/json',
        success: function (data) {
            if (data == "200") {
                status = true;
            }
        },
        error: function (data) {
            status = false;
        }
    })

    return status;
};

The problem am facing is , the content I retrived from the method show the namespace of the ViewModel. When I pass that to the new controlled the Viewmodel content is coming null.

Do I need to add anything to get/Pass the proper content ?

The Dummy method skeleton

    public ActionResult CastContent()
    {
        CastVM broadcastVM = new CastVM();


        return Json( broadcastVM);

    }
0

2 Answers 2

2

I have missed out the JsonRequestBehavior.AllowGet in my controller method, I have added and the results are coming perfectly.

public ActionResult CastContent()
    {
        CastVM broadcastVM = new CastVM();


        return Json( broadcastVM,JsonRequestBehavior.AllowGet);

    }

and also I have set the HTTP status as post in the jquery method

Sign up to request clarification or add additional context in comments.

Comments

1

Do something like that in the Controller:

public JsonResult QuickSave(BookEntry bookEntry) {
    YourViewModel model = new YourViewModel();
    return Json(model);
}

EDIT >> The associated Javascript code is :

$.ajax({
    type: "POST",
    url: 'The URL',
    data: 'The JSON data',
    dataType: "json",

    success: function (theViewModel) {
        // Do some JS stuff with your model
    },

    error: function (xhr, status, error) {
        // Do some error stuff
    }
});

2 Comments

Please see my edit. That's some working code, so I know it works.
Thanks Laurent PerrucheJ. As you have posted I have changed the Ajax method type to POST it is working apart from that we need to add the JsonRequestBehavior.AllowGet .I have updated the answer.

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.