asp.net mvc将Json对象从视图传递给控制器

时间:2014-07-08 11:19:50

标签: javascript ajax asp.net-mvc json

如何将JSON objec从AJAX成功函数传递给Controller? 我有这种情况:

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

我在第二次Ajax调用中成功获得了模型。但是当传递给window.location时,我在控制器动作中得到null参数。这是我的控制器动作代码:

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

如何通过javascript看起来像正确的代码从视图到控制器?或者我应该使用其他方法吗?

吼叫是我的模特:

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操作:

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

2 个答案:

答案 0 :(得分:1)

您是否尝试将JSON绑定到模型(我假设您的JSON与您的模型匹配)?

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

以下是对binding JSON to a model的介绍。

答案 1 :(得分:0)

我使用Session变量来获取GlassCompleteFrameView(PacModuleModel模型)中的模型并且工作完美。我在公共ActionResult GlassCompleteFrame(字符串b2bXml)中设置它并像下面一样使用它。

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