0
votes

How can I pass JsonResult object from javascript function in View to Controller Action without Ajax call - just javascript - window.location.href = url?

I get JsonResult object from Controller Action to javascript function via Ajax call. Then I want to pass this object back to other Controller Action but I get object with null reference properties.

My javascript function in View:

function order(model) {
    $('#details-container').html("<h2>Loading Complete Frame Module. Please wait...</h2>");
    $.p({
        url: '@Url.Action("CompleteFrameBrandDetails", "PacCompleteFrame")',
        data: { item: model },
        success: function (xml) {
            if (xml.Success) {

                $.p({
                    url: '@Url.Action("GlassCompleteFrame", "PacModule")',
                    data: JSON.stringify({ b2bXml: xml.Data }),
                    success: function (model) {
                        var pacModuleModel = {
                            Mode: model.Data.Mode,
                            IframeUrl: model.Data.IframeUrl.toString(),
                            CustomerNumber: model.Data.CustomerNumber.toString(),
                            ReadOnly: model.Data.ReadOnly,
                            GlassXml: model.Data.GlassXml.toString(),
                            Price: parseFloat(model.Data.Price),
                            Comission: model.Data.Comission.toString(),
                            Permissions: null,
                            Language: model.Data.Language.toString()
                        };
                        // here are all values in model.Data correct
                        // but then I can't figure out how to pass it to Controller Action without Ajax call - just with javascript command
                        var url = '@Url.Action("GlassCompleteFrameView", "PacModule",  "__view__")';
                        window.location.href = url.replace("__view__", model.Data); //pacModuleModel
                    }
                });
            } else {
                $.alert({
                    message: 'error while trying to load xml details'
                });
            }
        }
    });
}

My Controller Action:

    public ActionResult GlassCompleteFrameView(PacModuleModel model)
    {
        // here I get object module but 
        // model.CustomerNumber = null
        // model.GlasXml = null
        // model.Price = null
        // ...
        return View("Glass", model);
    }

I have also Model like this for automatic Json binding but dont work:

public enum ModuleMode
{
    ByProduct,
    ByRecipe
}

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; }
}
2
if i recall correctly, binding via the url.href means that you MUST pass in a querystring, formatted in the proper fashion. you can then decorate the controller action along the lines of public ViewResult GlassCompleteFrameView([Bind(Prefix="PacModuleModel ")]PacModuleModel model) - jim tollan
personally, with all the client side tooling available these days (knockout, requirejs etc), I can't see the advantage in trying to kludge solutions together that oppose good practice. however, your usecase may dictate a non clientside/javascript (jquery) approach to the issue. that said, maybe you're looking for a full page redirect?? - jim tollan
yes, I make full page redirect. - Korl

2 Answers

0
votes

Try this in controller

public JsonResult GlassCompleteFrameView(PacModuleModel model)
{
    // here I get object module but 
    // model.CustomerNumber = null
    // model.GlasXml = null
    // model.Price = null
    // ...
    return Json(model, JsonRequestBehavior.AllowGet);
}
0
votes

The problem was in model. It was more than 45000 char long. Now I use Session variable to get model in GlassCompleteFrameView(PacModuleModel model) and works perfect.

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