我改变了我的Job和CustomerEmployee Class之间的关系,我正在尝试发帖,我收到错误。使用断点我看到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 Class
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并没有将其从Angular Controller中删除
答案 0 :(得分:1)
"Error converting value 10 to type 'TexasExteriorSPA.Models.CustomerEmployee'
是相关的一行。您似乎正在将值10
传递为CustomerEmployeeAdminId
。 C#现在尝试将您的给定对象转换为JobViewModel
类型的对象,并在需要将10
转换为CustomerEmployee
时失败。你可以做这样的事情来解决它:
public class Job
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Int64 JobId { get; set; }
public int CustomerEmployeeAccountantId { get; set; }
}
答案 1 :(得分:1)
根据我所看到的情况,我认为弗洛里安是对的。您的代码应该类似于:
public class Job
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Int64 JobId { get; set; }
public int CustomerEmployeeAccountantId { get; set; }
public virtual CustomerEmployee CustomerEmployeeAccountant { get; set;}
}
您的对象应该引用Key(CustomerEmployeeAccountantId)和对象(CustomerEmployeeAccountant)。您的代码失败的原因是您的数据库链接位于Id之间,因此尝试将其分配给该属性。 &#34; Id&#34; property应该是一个int。
通过添加额外的虚拟属性,您可以告诉实体框架&#34;嘿,将外键的Id放在Id属性中,然后继续使用基于的关联数据填写CustomerEmployee对象外键关系。&#34;
然后,如果要访问CustomerEmployeeAccountant数据,它应该在您的API在标记为CustomerEmployeeAccountant的属性中返回的JSON对象中可用。
小心点。我注意到WebApi和EF在递归对象方面总是做得不好,所以在将数据传递给客户端之前,我会亲自将这些数据转储到ViewModel中。有很多方法可以关闭/改变行为,但我非常喜欢只返回你所需的最小化带宽。
此外,仅仅是个人偏好,但似乎是微软的大部分文档的目标,是长期优于int64。它是相同的数据类型,但我注意到使用long来命名一致性的趋势。 Int64在技术上是正确的。
更新:
查看截图,尝试从var data = {}更改变量名称; to var postData = {};。我无法看到你的其余代码,但看起来有几个变量名为data。这会导致冲突吗?