我試圖將角度表單上的一些JSON數據發佈到我的ASP.NET5 MVC6控制器操作。模型聯編程序似乎不工作。不知道我在這裏錯過了什麼。ASP.NET5的模型綁定問題MVC6
我的ASP控制器:
public class DefaultController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult SubmitTest(QTestViewModel model)
{
return Json("true");
}
}
我的角度控制器:
angular.module("testActiveMq", [])
.controller("MqTestController", ["$scope", "$http", function ($scope, $http) {
// Submit Form
$scope.submitForm = function() {
debugger;
var formData = (this.data) ? angular.toJson(this.data) : null;
if (formData && this.qForm && this.qForm.$valid) {
$http({
url: "/Default/SubmitTest",
data: formData,
method: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8"
})
.then(function successCallback(response) {
debugger;
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
debugger;
// called asynchronously if an error occurs
// or server returns response with an error status.
});
}
};
}])
我的視圖模型:
public class QTestViewModel
{
public string MqBrokerUri { get; set; }
public string ClientId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public int TotalRequests { get; set; }
public int MaxConcurrentRequests { get; set; }
public int DelayBetweenThreads { get; set; }
}
當我提出一個要求,HTTP頭..
POST /Default/SubmitTest HTTP/1.1
Host: localhost:50877
Connection: keep-alive
Content-Length: 225
Accept: application/json, text/plain, */*
Origin: http://localhost:50877
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:50877/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
我的表單數據看起來像這樣..
{"MqBrokerUri":"ssl://broker-uri:1616?transport.acceptInvalidBrokerCert=true","ClientId":"MqLoadTest","UserName":"myunm","Password":"mypwd","TotalRequests":100,"MaxConcurrentRequests":10,"DelayBetweenThreads":1}
我覺得我失去了一些東西超明顯。爲什麼我的JSON數據不能綁定到我的模型?當然,我不需要一個如此簡單的自定義模型綁定器?
嘗試改變'公共IActionResult SubmitTest(QTestViewModel模型)'來'公共IActionResult SubmitTest(弦模型)'作爲一個快速測試,看看它回來什麼 –
也嘗試'公共IActionResult SubmitTest([FromBody] QTestViewModel模型)' –
你缺少'JsonRequestBehavior.AllowGet'。更改爲'返回Json(「true」,JsonRequestBehavior.AllowGet);' –