将JsonResult对象从View中的javascript函数传递给Controller

时间:2014-07-15 09:42:52

标签: javascript jquery ajax asp.net-mvc

如何在没有Ajax调用的情况下将JsonResult对象从View中的javascript函数传递给Controller Action - 只需javascript - window.location.href = url?

我通过Ajax调用从Controller Action获取JsonResult对象到javascript函数。然后我想将此对象传递回其他Controller Action,但我得到了带有空引用属性的对象。

我在视图中的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'
                });
            }
        }
    });
}

我的控制器操作:

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

2 个答案:

答案 0 :(得分:0)

在控制器中试试

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

答案 1 :(得分:0)

问题在于模型。它超过45000焦炭长。现在我使用Session变量来获取GlassCompleteFrameView(PacModuleModel模型)中的模型并且工作正常。

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