我改变了我的工作和CustomerEmployee类之间的关系,我试图做一个帖子,我得到一个错误。使用一个断点,我发现这个Id没有返回到apiController。我做了一些搜索,我认为它似乎可能是一个JSON问题,不知道如何纠正它。我有6级编号的我想发表,但它们是建立相同的,所以我会告诉代码其中之一发布错误,“错误转换值ID”键入“到
错误消息
$id: "1"
Message: "The request is invalid."
ModelState: {$id:2, newJob.CustomerEmployeePMId:[,…], newJob.CustomerEmployeeAdminId:[,…],…}
$id: "2"
newJob.CustomerEmployeeAccountantId: [,…]
0: "Error converting value 10 to type 'TexasExteriorSPA.Models.CustomerEmployee'. Path 'CustomerEmployeeAccountantId', line 1, position 150."
newJob.CustomerEmployeeAdminId: [,…]
newJob.CustomerEmployeePMId: [,…]
newJob.CustomerEmployeeSuperintendentId: [,…]
视图
<select class="form-control" ng-options="customer.CustomerEmployeeId as customer.CustomerEmployeeFirstName + ' ' + customer.CustomerEmployeeLastName for customer in customerEmployeeArray | filter:{CustomerEmployeeRole : 'Accountant', CustomerId : currentItem.CustomerId} " ng-model="currentItem.CustomerEmployeeAccountantId">
<option value="" selected="selected">Customer Acct</option>
</select>
角控制器
//Post New Job
$scope.submitJob = function() {
var data = {
JobId: $scope.JobId,
CustomerEmployeeAdminId: $scope.currentItem.CustomerEmployeeAdminId
}
$http.post('/api/apiJob/PostNewJob', data).success(function (data, status, headers) {
console.log(data);
$scope.openNewJobModal.then(function (m) {
m.modal('hide');
});
});
};
WebApiConfig
// Web API configuration and services
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "JobApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "apiJob", id = RouteParameter.Optional }
);
apiController
// POST api/<controller>
public async Task<IHttpActionResult> PostnewJob([FromBody]JobViewModel newJob)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
using (var context = new ApplicationDbContext())
{
var job = new Job();
Mapper.CreateMap<JobViewModel, Job>();
Mapper.Map(newJob, job);
context.Jobs.Add(job);
await context.SaveChangesAsync();
return CreatedAtRoute("JobApi", new { job.JobId }, job);
}
}
作业类
public class Job
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Int64 JobId { get; set; }
public CustomerEmployee CustomerEmployeeAccountantId { get; set; }
}
CustomerEmployee类
public class CustomerEmployee
{
[Key]
public int CustomerEmployeeId { get; set; }
public string CustomerEmployeeFirstName { get; set; }
public string CustomerEmployeeLastName { get; set; }
public string CustomerEmployeeRole { get; set; }
public Int64? CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
更新,它看起来像ID是不可以这样做出来的角控制器的
您的模型'CustomerEmployeeAccountantId'上的属性的类型为'CustomerEmployee',即它是一个对象而非ad id。看起来你在Json中将它设置为10,所以模型绑定器说它不能将值'10'绑定到'CustomerEmployee'类型的属性。您可能需要将可以反序列化的json发送到'CustomerEmployee',或者将模型中的属性更改为int。 – 2014-10-28 14:42:58
我将如何反序列化它,我无法将其更改为int,因为我将失去我的关系 – texas697 2014-10-28 17:19:54
只要发布的JSON有效,它就会自动进行反序列化。这是模型活页夹的魔力。 – 2014-10-28 17:28:46