简体   繁体   English

如何通过 AJAX 将填充的自定义类从 javascript 传递到 .NET MVC 应用程序?

[英]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.

相关问题 如何通过ajax将一个对象数组从javascript传递到asp.net-mvc函数? - How can i pass an array of objects from javascript to asp.net-mvc function through ajax? 如何获得使用Ajax和Codeigniter填充的表单 - How do I get a form populated using ajax and codeigniter 通过javascript读取文件并通过ajax传递文件数据后,如何正确编码/解码文件? - How do I encode/decode a file correctly after reading it through javascript and pass the file data through ajax? 在ASP.NET MVC中,如何将ASP.NET剃须刀字符串传递到javascript onclick函数中? - In ASP.NET MVC how do I pass in ASP.NET razor strings into a javascript onclick function? 如何通过AJAX将数据从AJAX PHP结果传递到单独的PHP函数? - How do I pass data from an AJAX PHP result to a seperate PHP function through AJAX? 如何从javascript填充表中提取数据? - How do I pull data from a javascript populated table? 如何通过Jquery / Ajax将Javascript数组从Javascript传递到PHP? - how to pass a Javascript array from Javascript to PHP through Jquery/Ajax? 我如何使用javascript中的ajax调用将参数或ajax数据传递给python脚本并在成功时获得响应? - how do i pass parameter or ajax data to a python script using ajax call in javascript and get response if it is successful? 我如何从Firebase获取填充有数据的表 - How do I get a table populated with data from firebase 如何将字符串从MVC传递到Javascript以使用Ajax执行 - How can I Pass String from MVC to Javascript to Execute using Ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM