gpt4 book ai didi

c# - 如何将 Controller 操作绑定(bind)到引用的 dll 中的模型

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:51 24 4
gpt4 key购买 nike

我正在尝试将 JSON 发布到我的 .NET Core 项目中的 Controller 操作。

相关 Controller Action :

public IActionResult Post([FromBody]PrivateModel value) {/*Process value*/}

问题是 PrivateModel 来自引用的 dll。因此,自动模型绑定(bind)失败,value 始终为 null。

我认为这是模型不是本地模型的一个因素,因为如果我在我的项目中创建 PrivateModel 结构的副本并改用它,数据就会正确绑定(bind)。使用这个精简副本不是一个可行的解决方案,因为我需要使用 PrivateModel 的函数。

有没有办法让模型绑定(bind)与引用的类一起工作?

最佳答案

=> 查看代码,此引用目的。

  1. 您的 API Controller ,参数名称与 ajax 数据相同,例如 [ data: param ] 参数

  2. 您的 Json post 数据成员与您的引用 dll 类属性相同。

API Controller

//Also you can use ActionResult
public HttpResponseMessage controller(Parameter param)
{
return Json // Return Json
}

引用 DLL 类示例

public class Parameter
{
public string publish { get; set; }
public string user { get; set; }
}

按钮上的 Javascript 函数点击喜欢

 function RunDemand() {
var param = '{ publish : "On Demand",user : "Ankit" }';
$.ajax({
url: "/api/controller",
type: "POST",
data: param,
contentType: "application/json; charset = utf-8",
success: function (data) {
if (data.success) {
//Do Process
}
else {
//Do Process
}
}
});
}

关于c# - 如何将 Controller 操作绑定(bind)到引用的 dll 中的模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48644600/

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