Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How can I pass JSON objec from AJAX success function to Controller? I have this situation:

function order(model) {
    $.p({
        url: '@Url.Action("CompleteFrameBrandDetails", "PacCompleteFrame")',
        data: { item: model },
                        var pacModuleModel = {
                            CustomerNumber: model.Data.CustomerNumber,
                            Language: model.Data.Language,
                            Comission: model.Data.Comission,
                            GlassXml: model.Data.GlassXml,
                            Price: model.Data.Price,
                            ReadOnly: model.Data.ReadOnly,
                            Mode: model.Data.Mode,
                            IframeUrl: model.Data.Mode
                        };
                        var url = '@Url.Action("GlassCompleteFrameView", "PacModule", new { b2bXml = "__xml__" })';
                        $('#details-container').html(url.replace("__xml__", JSON.stringify(model.Data))); //"<h2>Loading Complete Frame Module. Please wait...</h2>"
                        window.location.href = url.replace("__xml__", JSONstringify(pacModuleModel)); //JSON.stringify(result.Data.GlassXml); JSON.stringify(result.Data)
                    }
                });

            } else {
                $.alert({
                    message: 'error while trying to load xml details'
                });
            }
        }
    });

I successfully get model in second Ajax call. But when passing to window.location I get null parameter in controller action. Here is my controller action code:

    public ActionResult GlassCompleteFrameView(JsonResult model)
    {
        return View("Glass", model);
    }

How should look like correct code to get my model from view to controller via javascript? Or should I use some other approach?

And bellow is my model:

public partial class PacModuleModel
{
    private PacPermissionModel permissionModel;


    public ModuleMode Mode { get; set; }
    public string IframeUrl { get; set; }
    public string CustomerNumber { get; set; }
    public bool ReadOnly { get; set; }
    public string GlassXml { get; set; }
    public double? Price { get; set; }
    public string Comission { get; set; }
    public PacPermissionModel Permissions
    {
        get
        {
            if (permissionModel == null)
            {
                permissionModel = new PacPermissionModel();
            }
            return permissionModel;
        }
    }
    public string Language { get; set; }
}

GlassCompleteFrame action:

    public ActionResult GlassCompleteFrame(string b2bXml)
    {
        string mode = "5";

        //If the Store isn't selected, redirect to HomePage
        if (string.IsNullOrEmpty(_workContext.SelectedCustomerNumber))
        {
            return RedirectToRoute("HomePage");
        }
        else
        {
            PacModuleModel model = new PacModuleModel();
            model.CustomerNumber = _workContext.SelectedCustomerNumber;
            model.Language = _workContext.WorkingLanguage.UniqueSeoCode;
            model.Comission = "";
            model.GlassXml = b2bXml.Replace("\"", "\\\"");
            int index = b2bXml.IndexOf("<price>") + "<price>".Length;
            string p = b2bXml.Substring(index, b2bXml.IndexOf("</price>") - index);
            model.Price = Convert.ToDouble(p, System.Globalization.CultureInfo.InvariantCulture);
            model.ReadOnly = false;
            model.Mode = ModuleMode.ByProduct;
            model.IframeUrl = "http://ItkCompleteConfiEmbedded.aspx?lang=" + _workContext.WorkingLanguage.LanguageCulture;

            return new JsonResult()
            {
                Data = new
                {
                    Success = true,
                    Data = model
                }
            };
        }
    }
share|improve this question

2 Answers 2

I use Session variable to get model in GlassCompleteFrameView(PacModuleModel model) and works perfect. I set it in public ActionResult GlassCompleteFrame(string b2bXml) and use it like bellow.

public ActionResult GlassCompleteFrameView(PacModuleModel model)
{
    model = Session["xml"] as PacModuleModel;
    return View("Glass", model);
}
share|improve this answer

Have you tried binding the JSON to your model (I'm assuming your JSON matches your model)?

public ActionResult GlassCompleteFrameView(PacModuleModel model)
{
    return View("Glass", model);
}

Here is an introduction to binding JSON to a model.

share|improve this answer
    
WooHoo thank you for the answer. Yes my JSON matches to my model. I chanhe type of parameter like you suggested but I still get model = null. Something else is missing or it is not ok, but i dont see what :( Thank you also for the article. I hope that I will figured out. If you have some more hints how to solve this I will be gratefull. –  Korl Jul 8 '14 at 16:11
    
@Korl could you add a sample of your JSON to your question? –  WooHoo Jul 8 '14 at 16:23
    
If I pass simple string then works, even vith JSON object like model.Data.Language but not with model.Data.GlassXml :( GlassXml is pretty long. Maybe is this cause of problem. I don't have idea. Please help. –  Korl Jul 8 '14 at 16:34
    
WooHoo I added my JSON object in my question. Please see last function. –  Korl Jul 8 '14 at 16:44
    
Your JSON has to have the same properties as your model for the automatic binding to work. If you look at the example link you can see that in JavaScript the author builds a model and then posts this to the action method. The model in the action method has the same name as the JSON object, so auto binding knows to match, name with model.name, age with mode.age. –  WooHoo Jul 8 '14 at 16:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.