![](/img/trans.png)
[英]Passing a complex model to controller using AJAX in ASP.NET Core MVC
[英]Post AngularJS model to complex model in ASP.NET MVC controller
我正在嘗試用JavaScript創建一個模型,並通過$ http.post將其發布到ASP.NET MVC控制器方法。 檢查發布的JSON時,將填充我的Address
屬性。 但是,當命中ASP.NET MVC控制器時,所有相應的Address屬性都為null。
角度控制器方法:
$scope.submitForm = function(isValid) {
alert("Form is valid!");
var member = {
TempCardNumber: $scope.tcardno,
Title: $scope.title,
FirstName: $scope.firstName,
LastName: $scope.lastName,
Address: [
{
AddressLine1: $scope.address1,
AddressLine2: $scope.address2,
AddressLine3: $scope.address3,
AddressLine4: $scope.address4,
TownCity: $scope.towncity,
County: $scope.county,
PostCode: $scope.postcode,
PhoneNumber: $scope.phone
}
],
EmailAddress: $scope.email
};
$http({
method: 'POST',
url: '/Home/Index',
data: member
}).success(function (data, status, headers, config) {
});
}
發表JSON:
{"TempCardNumber":"633341677489986320","Title":"Mr","FirstName":"test","LastName":"user","Address":[{"AddressLine1":"8 Eaton Close","AddressLine2":" Hatton","TownCity":" Derby","County":" Derbyshire","PostCode":" DE65 5ED","PhoneNumber":"01234567890"}],"EmailAddress":"test@gmail.com"}
ASP.NET MVC控制器:
public ActionResult Index([FromBody]MemberModel model)
{
// do stuff
}
我已經嘗試了字符串化JSON,將其作為我的Angular帖子中的data: member
傳遞,並在ASP.NET MVC方法上使用[FromBody]
,但總是, model.Address
屬性為null。
無論如何我可以在不將每個屬性發布到我的ASP.NET MVC控制器的情況下執行此操作嗎?
編輯:為了清楚起見,添加了MemberModel POCO
// MemberModel class
public string Title {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public Address Address{get;set;}
public string TempCardNumber {get;set;}
public string Email {get;set;}
// Address POCO
public string AddressLine1 {get;set;}
public string AddressLine2 {get;set;}
public string AddressLine3 {get;set;}
public string AddressLine4 {get;set;}
public string TownCity {get;set;}
public string County {get;set;}
public string Postcode {get;set;}
public string PhoneNumber {get;set;}
確保在控制器方法上使用[HttpPost]
屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.