【发布时间】:2017-09-30 06:27:21
【问题描述】:
所以我一直在学习一些 .net 核心,并且正在用它构建一个 API。通常我会使用 angular 并在那里发送请求。
我有以下角度 sn-p:
//The data I need to send.
$scope.PersonalInfo = {
firstName: '',
lastName: '',
companyName: '',
email: '',
password: '',
confirmPassword: '',
imageLink: ''
};
然后是相同数据的后端模型:
public class UserProfileModel
{
public string userID { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string companyName { get; set; }
public string email { get; set; }
public string password { get; set; }
public string confirmPassword { get; set; }
public string imageLink { get; set; }
}
最后是应该发送它的方法:
$scope.UpdateProfile = function () {
var profile = JSON.stringify($scope.PersonalInfo);
$http.post('/api/Profile/Users', profile).then(function (response) {
debugger;
$log.log(profile);
$log.log(response);
});
};
无论我做了多少更改,(将数据作为字符串化 JSON 发送,或发送范围对象),当请求到达控制器时,我都会得到以下结果:
$scope.PersonalInfo 在发送请求之前已充满数据。
非常感谢任何帮助。提前致谢。
【问题讨论】:
-
发送前无需对 JS 对象进行字符串化(由
$http自动完成)。你试过没有字符串化吗? -
是的,我做了,不管有没有它都没有绑定。
-
在 PascalCase 中创建 C# UserProfileModel ... UserId、FirstName 等...这可能与 JSON 序列化程序设置有关。
-
你能把
userId: ''加到PersonalInfo看看是否可行,它可能正在寻找所有属性。 -
你知道如何使用谷歌浏览器的网络流量工具来检查请求/响应吗?
标签: javascript angularjs asp.net-core asp.net-core-mvc