gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:17:05 25 4
gpt4 key购买 nike

如何将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com