我使用angular' s $ http:
$http({
url: '/api/Movies/UpdateMovie',
method: "POST",
data: movie,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
})
检查客户端我看到电影正确填充,但是当向服务器发送数据时,电影模型没有填充发布的数据(或者可能没有发布数据),我使用web api进行服务。我服务器上的电影模型是:
public class Movie
{
public int Id { get; set; }
[Required]
[StringLength(256, ErrorMessage = "Maximum Title length is 256.")]
public string Title { get; set; }
[Required]
[StringLength(256, ErrorMessage = "Maximum Director length is 256.")]
public string Director { get; set; }
[Required]
public decimal Price { get; set; }
}
如何将电影作为数据传递?
答案 0 :(得分:0)
您可以将对象解析为表单数据,也许这个要点https://gist.github.com/ghinda/8442a57f22099bdb2e34对您有用,无论如何您可以将内容类型更改为application / json,如下所示:
标题:{'Content-Type':'application / json'}
答案 1 :(得分:0)
使用此功能:
var param = function (obj) {
var query = '', name, value, fullSubName, subName, subValue, innerObj, i; for (name in obj) {
value = obj[name]; if (value instanceof Array) {
for (i = 0; i < value.length; ++i)
{ subValue = value[i]; fullSubName = name + '[' + i + ']'; innerObj = {}; innerObj[fullSubName] = subValue; query += param(innerObj) + '&'; }
} else if (value instanceof Object) {
for (subName in value)
{ subValue = value[subName]; fullSubName = name + '[' + subName + ']'; innerObj = {}; innerObj[fullSubName] = subValue; query += param(innerObj) + '&'; }
} else if (value !== undefined && value !== null) query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
} return query.length ? query.substr(0, query.length - 1) : query;
}; // Override $http service's default transformRequest $httpProvider.defaults.transformRequest = [function(data) { return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data; }];});
并设置
data: param(logOn), headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
现在可行。
答案 2 :(得分:0)
在您的代码中尝试:
var movie = {};
movie.Title = "Title";
movie.Director = "Director";
movie.Price = 0.50;
并删除:headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
$http({
url: '/api/Movies/UpdateMovie',
method: "POST",
data: movie
});