2016-05-23 35 views
1

我想在ASP.Net Core 1.0中发送带有几个参数的请求(我不知道它是否重要)。ASP.Net MVC不映射ajax调用的参数

我有以下模型:

[DataContract] 
public class RegistrationConfigContract 
{ 
    [DataMember] 
    public string ServiceType { get; set; } 
    [DataMember] 
    public int BatchTimeout { get; set; } 
    [DataMember] 
    public int BatchSize { get; set; } 
    [DataMember] 
    public ActorFinishingTypeEnum FinishingType { get; set; } 
} 

我有以下控制方法:

public void SendRegistration(string nodeName, NodeTypeEnum nodeType, RegistrationConfigContract model) 
{ 

} 

我也有尝试调用它JS:

function sendRegistration(nodeName, nodeType) { 
    var model = { 
     ServiceType: $("#serviceType").html(), 
     BatchTimeout: $("#batchTimeout").html(), 
     BatchSize: $("#batchSize").html(), 
     FinishingType: $("#finishingType").html() 
    }; 

    var request = { 
     nodeName: nodeName, 
     nodeType: nodeType, 
     model: model 
    } 

    //$.post('@Url.Action("SendRegistration")', request); 
    $.ajax({ 
     url: '@Url.Action("SendRegistration")', 
     type: "POST", 
     dataType: 'json', 
     data: JSON.stringify(request), 
     async: false, 
     cache: false, 
     traditional: true, 
     contentType: 'application/json' 
    }); 
} 

但在服务器根据Chrome/Edge调试器的说法,我总是会得到空值,而在JS端则一切正常。

我在这里做错了什么?我试图使用代码我Google搜索这个问题,但它不工作。

原始HTTP请求:

POST http://localhost:8080/Configuration/SendRegistration HTTP/1.1 
Host: localhost:8080 
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0 
Accept: application/json, text/javascript; q=0.01 
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3 
Accept-Encoding: gzip, deflate 
DNT: 1 
Content-Type: application/json 
X-Requested-With: XMLHttpRequest 
Referer: http://localhost:8080/Configuration 
Content-Length: 156 
X-Compress: 1 
Proxy-Authorization: 6e9b34bd44817cf5c254e1ccb4fe7b31ecd526ea9e025e06a16baca626af6be0ea7fa102c2205f0e 
Connection: keep-alive 

{"nodeName":"zhdp","nodeType":"DocumentProducer","model":{"ServiceType":"jjjjjj","BatchTimeout":"33535","BatchSize":"6666","FinishingType":"UpdateMessage"}} 

回答

1

问题出在模型绑定... ASP.NET Core中的模型绑定是不同的。 您应该用适当的属性标记您的动作参数,如 [FromBody]或[FromForm]。此外,你可以有一个正常的形式邮寄或阿贾克斯 职位,但不能同时...

此处了解详情:https://andrewlock.net/model-binding-json-posts-in-asp-net-core/

+0

我找到了答案我自己,但无论如何感谢链接,它有一些有趣info。 –

0

看来,这是一个错误ASP.Net Core(或功能)。我混合了查询字符串参数和模型。在完成之后,它可以绑定查询字符串,但不能绑定请求正文。加入[FromBody]属性后,一切正常。

JS:

function sendRegistration(nodeName, nodeType) { 
    var model = { 
     ServiceType: $("#serviceType").html(), 
     BatchTimeout: $("#batchTimeout").html(), 
     BatchSize: $("#batchSize").html(), 
     FinishingType: $("#finishingType").html() 
    }; 

    $.ajax({ 
     url: '@Url.Action("SendRegistration")?nodeName={0}&nodeType={1}'.f(nodeName, nodeType), 
     type: "POST", 
     dataType: 'json', 
     data: JSON.stringify(model), 
     cache: false, 
     traditional: true, 
     contentType: 'application/json' 
    }); 
} 

其中fstring.Format类似物。和C#的一面:

[HttpPost] 
public void SendRegistration(string nodeName, NodeTypeEnum nodeType, [FromBody] RegistrationConfigContract model) 
{ 

} 

[HttpPost]在这里没有必要,但它显示我的意图,而不是它的缺乏更好。