javascript - asp.net mvc 将 Json 对象从 View 传递到 Controller

标签 javascript ajax asp.net-mvc json

如何将 JSON 对象从 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 时,我在 Controller 操作中得到 null 参数。这是我的 Controller 操作代码:

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

正确的代码应该如何通过 JavaScript 将我的模型从 View 获取到 Controller ?或者我应该使用其他方法?

下面是我的模型:

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

最佳答案

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

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

这里介绍binding JSON to a model .

关于javascript - asp.net mvc 将 Json 对象从 View 传递到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24630463/

相关文章:

javascript - 如何在 setInterval 方法中更新 HighChart 系列

javascript - jQuery 从外部源下载图像

javascript - JS - 带图像的单选按钮

asp.net-mvc - 关于在 Controller 和扩展方法中访问 ASP.NET MVC Session[] 数据的建议?

c# - 如何扩展 Html.Raw 以在显示前清理危险的 HTML 数据

Javascript/XML - 获取节点名称

jquery - 在 AJAX 简写方法中设置传统属性

javascript - 为什么 jquery ajax 调用 Controller 没有发现我的异常?

javascript - Web 方法中的可选参数

c# - 为什么 MVC 会在看似没有 Null Reference 的情况下抛出 NullReferenceException?