将错误,“将值'ID'转换为类型时出错”

德州697

我更改了我的Job和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; }
}

客户员工类别

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中 图片 图片2

Actias

Based on what I can see, I think that Florian has it right. Your code should probably be something like:

public class Job
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public Int64 JobId { get; set; }

    public int CustomerEmployeeAccountantId { get; set; }
    public virtual CustomerEmployee CustomerEmployeeAccountant { get; set;}
}

Your object should have a reference to both the Key (CustomerEmployeeAccountantId) and the object (CustomerEmployeeAccountant). The reason your code is failing is that your DB link is between the Id's, hence the 10 that's trying to be assigned to the property. The "Id" property should be an int.

By adding the additional virtual property, you're telling Entity Framework "Hey, put the Id of the Foreign Key in the Id property and then go ahead and fill out CustomerEmployee object with the associated data based on the Foreign Key relationship."

Then, if you want to access the CustomerEmployeeAccountant data, it should be available in the JSON object returned by your API in a property marked CustomerEmployeeAccountant.

Just be careful. I've noticed that WebApi and EF don't always do so well with recursive objects, so I'd personally dump this data into a ViewModel before it's passed to the client. There are ways to turn it off/change the behaviour, but I'm huge fan of only returning exactly what you need to minimize bandwidth.

Also, just a personal preference, but it seems to be the direction that most of Microsoft's documentation is headed, is to favor long over int64. It's the same data type, but I've noticed the trend of using long for naming consistency. Int64 is technically correct though.

UPDATE:

查看屏幕快照,尝试从var data = {}更改变量名。到var postData = {};。我看不到其余的代码,但看起来有几个名为data的变量。那会引起冲突吗?

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将varchar值'@id'转换为数据类型int时转换失败

来自分类Dev

将varchar值'where name_id in('转换为数据类型int时,转换失败

来自分类Dev

C#Json反序列化异常(“将值“ id”转换为类型'Eng_Tab.JsonData'时出错。路径'[0]',第1行,位置5”。)

来自分类Dev

C#JSON错误(将值“用户名”转换为类型时出错)

来自分类Dev

将nvarchar转换为int数据类型时出错

来自分类Dev

将LINQ匿名类型转换为IList <>时出错

来自分类Dev

将LINQ匿名类型转换为IList <>时出错

来自分类Dev

将类型强制转换为结构指针时出错

来自分类Dev

将数据类型从varchar转换为数值时出错

来自分类Dev

将数据类型NVarchar转换为int时出错

来自分类Dev

将数据类型 varchar 转换为 float 时出错。?

来自分类Dev

将数据类型varchar转换为varbinary时出错。转换为整数时的MSSQL

来自分类Dev

调用存储过程错误:将数据类型nvarchar转换为datetime时出错

来自分类Dev

使用XML路径方法的SQL错误,将数据类型varchar转换为float时出错

来自分类Dev

我的错误在哪行?将数据类型nvarchar转换为数值时出错

来自分类Dev

调试并找到错误“将数据类型varchar转换为实型时出错”

来自分类Dev

我收到一个错误“将数据类型nvarchar转换为实数时出错”。

来自分类Dev

在执行查询时将数值转换为数据类型数值的算术溢出错误

来自分类Dev

在触发器获取错误中,将数据类型varchar转换为数值时出错

来自分类Dev

将类型'UICollectionReusableView'的值强制转换为'MyApp.CSSectionHeader'时出错

来自分类Dev

将值转换为我的JSON类型'System.Collections.Generic.List`时出错

来自分类Dev

将值转换为我的json中的类型'System.Collections.Generic.List`时出错

来自分类Dev

Swift错误:强制转换时无法将B类型的值转换为A类型

来自分类Dev

将一般类型转换为具体类型时出错

来自分类Dev

使用子字符串转换将数据类型varchar转换为数值时出错

来自分类Dev

获取“将数据类型 varchar 转换为数字时出错”。即使转换后

来自分类Dev

查询给出错误,指出将值转换为整数时存在问题

来自分类Dev

错误“将varchar值'ABC'转换为数据类型int时转换失败。”

来自分类Dev

错误:将字节码转换为dex时出错

Related 相关文章

  1. 1

    将varchar值'@id'转换为数据类型int时转换失败

  2. 2

    将varchar值'where name_id in('转换为数据类型int时,转换失败

  3. 3

    C#Json反序列化异常(“将值“ id”转换为类型'Eng_Tab.JsonData'时出错。路径'[0]',第1行,位置5”。)

  4. 4

    C#JSON错误(将值“用户名”转换为类型时出错)

  5. 5

    将nvarchar转换为int数据类型时出错

  6. 6

    将LINQ匿名类型转换为IList <>时出错

  7. 7

    将LINQ匿名类型转换为IList <>时出错

  8. 8

    将类型强制转换为结构指针时出错

  9. 9

    将数据类型从varchar转换为数值时出错

  10. 10

    将数据类型NVarchar转换为int时出错

  11. 11

    将数据类型 varchar 转换为 float 时出错。?

  12. 12

    将数据类型varchar转换为varbinary时出错。转换为整数时的MSSQL

  13. 13

    调用存储过程错误:将数据类型nvarchar转换为datetime时出错

  14. 14

    使用XML路径方法的SQL错误,将数据类型varchar转换为float时出错

  15. 15

    我的错误在哪行?将数据类型nvarchar转换为数值时出错

  16. 16

    调试并找到错误“将数据类型varchar转换为实型时出错”

  17. 17

    我收到一个错误“将数据类型nvarchar转换为实数时出错”。

  18. 18

    在执行查询时将数值转换为数据类型数值的算术溢出错误

  19. 19

    在触发器获取错误中,将数据类型varchar转换为数值时出错

  20. 20

    将类型'UICollectionReusableView'的值强制转换为'MyApp.CSSectionHeader'时出错

  21. 21

    将值转换为我的JSON类型'System.Collections.Generic.List`时出错

  22. 22

    将值转换为我的json中的类型'System.Collections.Generic.List`时出错

  23. 23

    Swift错误:强制转换时无法将B类型的值转换为A类型

  24. 24

    将一般类型转换为具体类型时出错

  25. 25

    使用子字符串转换将数据类型varchar转换为数值时出错

  26. 26

    获取“将数据类型 varchar 转换为数字时出错”。即使转换后

  27. 27

    查询给出错误,指出将值转换为整数时存在问题

  28. 28

    错误“将varchar值'ABC'转换为数据类型int时转换失败。”

  29. 29

    错误:将字节码转换为dex时出错

热门标签

归档