[英]How do I get pass a populated custom class from javascript through AJAX to .NET MVC app?
I have the following .net class in c#:我在 C# 中有以下 .net 类:
public class BusinessModel
{
public string BlobName { get; set; }
public string NewName { get; set; }
}
And I have the following action in my MVC Ajax Controller:我在我的 MVC Ajax 控制器中有以下操作:
[HttpPost]
public ActionResult DoBusiness(string handle, BusinessModel businessData)
{
// Do stuff with the business data.
}
First I try to use the following Javascript code to invoke this:首先,我尝试使用以下 Javascript 代码来调用它:
var bd = JSON.stringify({ BlobName: "blob", NewName: "new" });
$.ajax({
type: "POST",
url: "/Ajax/DoBusiness",
data: { handle: "MyHandle", businessData: bd },
success: businessComplete,
error: businessFailed
});
And in this case, using fiddler to view, the following is how the data is rendered on the request:而本例中,使用fiddler查看,如下是数据在请求上的呈现方式:
handle=MyHandle&businessData=%7B%22BlobName%22%3A%22blob%22%2C%22NewName%22%3A%22new%22%7D handle=MyHandle&businessData=%7B%22BlobName%22%3A%22blob%22%2C%22NewName%22%3A%22new%22%7D
Which is the following decoded:这是以下解码:
handle=MyHandle&businessData={"BlobName":"blob","NewName":"new"} handle=MyHandle&businessData={"BlobName":"blob","NewName":"new"}
But when I use the Visual Studio debugger to look at the values actually passed to the controller action, the value of the parameter "handle" is "MyHandle" as expected, but the value of the parameter "businessData" is null.但是当我使用 Visual Studio 调试器查看实际传递给控制器操作的值时,参数“handle”的值为“MyHandle”,但参数“businessData”的值为空。
Now I alter the Javascript code to read as follows (merely removing the JSON.stringify call):现在我将 Javascript 代码更改为如下(仅删除 JSON.stringify 调用):
var bd = { BlobName: "blob", NewName: "new" };
$.ajax({
type: "POST",
url: "/Ajax/DoBusiness",
data: { handle: "MyHandle", businessData: bd },
success: businessComplete,
error: businessFailed
});
In this case, the data (shown by fiddler) is rendered in the request as:在这种情况下,数据(由 fiddler 显示)在请求中呈现为:
handle=MyHandle&businessData%5BBlobName%5D=blob&businessData%5BNewName%5D=new handle=MyHandle&businessData%5BBlobName%5D=blob&businessData%5BNewName%5D=新
Which is the following decoded:这是以下解码:
handle=MyHandle&businessData[BlobName]=blob&businessData[NewName]=new handle=MyHandle&businessData[BlobName]=blob&businessData[NewName]=new
And in this case, the Visual Studio debugger still shows that "handle" is set to "MyHandle" as expected, but instead of being null, the "businessData" parameter is filled with an actual instance, but both properties "BlobName" and "NewName" are set to null.在这种情况下,Visual Studio 调试器仍然显示“handle”按预期设置为“MyHandle”,但不是 null,而是“businessData”参数填充了一个实际实例,但属性“BlobName”和“ NewName”设置为空。
Finally the question: What am I doing wrong?最后一个问题:我做错了什么? How can I actually get that class properly populated in the controller action?我怎样才能真正在控制器操作中正确填充该类?
EDIT In response to comment on possible duplicate: The possible duplicate, titled "Send JSON data via POST (ajax) and receive json response from Controller (MVC)" specifically addresses how to send instances of models bound with MVC binding.编辑响应对可能重复的评论:可能的重复,标题为“通过 POST (ajax) 发送 JSON 数据并从控制器 (MVC) 接收 json 响应”专门解决了如何发送与 MVC 绑定绑定的模型实例。 My question is more not related to an object that might be MVC-bound.我的问题更多地与可能受 MVC 绑定的对象无关。 I am curious about how to populate ANY object, that might be arbitrarily provided.我很好奇如何填充可能任意提供的任何对象。
EDIT In response to Stephen's comment: This is a simplified scenario.编辑回应斯蒂芬的评论:这是一个简化的场景。 The real-life actual use-case has an overlapping set of Ajax calls, and this one is called in response to a previous one which has passed along an arbitrary object constructed on the server which it wants back in the subsequent call (this example).现实生活中的实际用例有一组重叠的 Ajax 调用,这个调用是为了响应前一个调用,该调用传递了在服务器上构造的任意对象,它希望在后续调用中返回(本示例) . Providing that fully-fleshed real-life use case would, in my opinion, make this question unduly long, so pardon the simplification, and I hope this explanation will serve to appropriately frame the context.在我看来,提供完整的现实生活用例会使这个问题过分冗长,所以请原谅简化,我希望这种解释将有助于适当地构建上下文。
Please see Stephan Muecke's comment.请参阅 Stephan Muecke 的评论。 He provided the answer that works at least for now.他提供了至少目前有效的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.