HTML作为参数JSON MVC
我需要使用HTML作为参数JSON MVC,html,json,asp.net-mvc-2,parameters,Html,Json,Asp.net Mvc 2,Parameters,我需要使用json asp.net mvc2将html作为参数传递 我试着这样做: $.ajax({ type: "POST", dataType: "json", contentType: 'application/json; charset=utf-8', url: "/DefaultView/GeneratePDF", data: { "pdf": JSON.
json asp.net mvc2将html作为参数传递
我试着这样做:
$.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json; charset=utf-8',
url: "/DefaultView/GeneratePDF",
data: { "pdf": JSON.parse($("#displayContainer").html()) },
success: function (data) {
//nothing here
}
});
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
但它抛出了“uncaughtsyntaxerror:unexpectedtoken<”错误
使用stringify()我在这里得到null:
[HttpPost]
[ValidateInput(false)]
public virtual ActionResult GeneratePDF(string pdf)
{
pdf <---
[HttpPost]
[验证输入(错误)]
公共虚拟操作结果生成器pdf(字符串pdf)
{
pdf使用JSON的字符串化方法:
$.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json; charset=utf-8',
url: "/DefaultView/GeneratePDF",
data: { "pdf": JSON.stringify($("#displayContainer").html()) }, //change to stringify
success: function (data) {
//nothing here
}
});
编辑:如果您使用的是不支持绑定到JSON的MVC 2模型。请参阅本文-我通过此链接解决了我的问题:
…我需要引用Microsoft.Web.Mvc
,并在我的应用程序\u start(global.asax)中添加如下调用:
$.ajax({
type: "POST",
dataType: "json",
contentType: 'application/json; charset=utf-8',
url: "/DefaultView/GeneratePDF",
data: { "pdf": JSON.parse($("#displayContainer").html()) },
success: function (data) {
//nothing here
}
});
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
我还需要利用
正如@Jason Roell所说,我们不能将stringify与MVC 2一起使用。模型绑定器不知道如何正确解析值
毕竟,一切都按预期进行
谢谢
我希望它能帮助一些人。你真的进入了那个方法吗?是的…bud pdf是空的,如果我调用console.log(JSON.stringify($(“#displayContainer”).html())
它会返回元素。当你运行JSON.stringify($(“#displayContainer”).html()时,你会得到什么在开发人员工具的控制台中?您看到返回结果了吗?是的…它会按预期返回。请尝试添加contentType:'application/x-www-form-urlencoded'