将JsonResult对象从视图中的javascript函数传递给控制器

pass JsonResult object from javascript function in View to Controller

本文关键字:函数 控制器 javascript 对象 JsonResult 视图      更新时间:2023-09-26

如何将JsonResult对象从视图中的javascript函数传递到控制器动作而没有Ajax调用-只是javascript - window.location.href = url?

我得到JsonResult对象从控制器动作到javascript函数通过Ajax调用。然后我想把这个对象传递给其他控制器动作,但我得到的对象引用属性为空。

View中的javascript函数:

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);
    }

我也有这样的模型自动Json绑定,但不工作:

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; }
}

在控制器

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);
}

问题出在模型上。它超过45000字符长。现在我使用会话变量在GlassCompleteFrameView(PacModuleModel模型)中获得模型,并且工作完美。

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