0

When updating an entity in the database, it throws an exception.

"Unexpected character encountered while parsing value: T. Path '', line 0, position 0."

 public void UpdateMainEditor(Photogallery entity)
        {

             _dbContext.Entry(entity).State = EntityState.Modified;

            _dbContext.SaveChanges();
        }

Unable to update entity and save it after SaveChanges() gives an exception at endpoint

Endpoint Put

public async Task<IActionResult> Put([FromODataUri]int key, [FromBody] Photogallery oData)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (key != oData.Id)
            {
                return BadRequest();
            }

            try
            {
                await _targetHelper.Update(oData, User);

                return Ok(oData);
            }
            catch (Exception ex)
            {
                return StatusCode(500, ex.Message);
            }
}

Model Photogalleries

public class Photogallery : IBaseEntity<int>, ITargetItem
    {
        [Key]
        public int Id { get; set; }
        [Multilingual]
        [Required, StringLength(255)]
        public string Name { get; set; }
        [Multilingual]
        [StringLength(255)]
        public string Description { get; set; }
        [Required]
        public DateTime PublicationStart { get; set; }
        public int? CompanyId { get; set; }
        public bool IsGroup { get; set; }

        public virtual Company Company { get; set; }

        public virtual List<PhotogalleryFile> Files { get; set; }

        public virtual List<TranslatePhotogallery> TranslatePhotogalleries { get; set; }
    }

Model TranslatePhotogallery

 public class TranslatePhotogallery
    {
        [Key]
        public int Id { get; set; }

        public int PhotogalleryId { get; set; }

        public TranslateLanguage Language { get; set; }

        public string TranslateField { get; set; }


        public virtual Photogallery Photogallery { get; set; }

    }

TranslateLanguage is enum!!!! In database nvarchar! :D

Body in request

{
    "id": 46,
    "name": "Test Postman 2, 3,4",
    "description": "Text",
    "publicationStart": "2019-03-22T08:49:46.981Z",
    "companyId": 4,
    "isGroup": true,
    "translatePhotogalleries": [{
        "language": "Ukrainain",
        "translateField": "Some Text"
    }]
}

enter image description here

  • Have you taken a look at this? [Unexpected character encountered while parsing value](https://stackoverflow.com/questions/23259173/unexpected-character-encountered-while-parsing-value) – Tyler Roper Jun 14 '19 at 14:48
  • @TylerRoper, I do not want to use DeserializeObject. Updates should occur without explicit deserialize. – Андрей Ляшенко Jun 14 '19 at 15:04
  • There are other answers/solutions there besides the one that's accepted, noting that this error occurs if the JSON is malformed or incorrect. Perhaps you can share your JSON. – Tyler Roper Jun 14 '19 at 15:05
  • @TylerRoper, look at the description of the problem. There in API there is a check on ModelState.IsValid. І in the description there is also JSON. – Андрей Ляшенко Jun 14 '19 at 15:10
  • I have tried to invoke from POSTMAN and there is't any trouble with request model, just to make sure did you set content type for your request? – H. Herzl Jun 14 '19 at 16:14
  • @H.Herzl, type content is application/json. – Андрей Ляшенко Jun 14 '19 at 16:28
  • It seems like an encoding trouble, did you check this: https://github.com/aspnet/Mvc/issues/7188 ? – H. Herzl Jun 14 '19 at 16:40
  • @H.Herzl, Some entities I have updated in the database without problems, but the problem with the essence of the photo galleries! – Андрей Ляшенко Jun 14 '19 at 16:48
  • In that case I'll need to replicate the same models in order to debug the exception, by the way, did you checked the required attributes for model properties? The error messages seems like there was an exception to cast, I have tried in my workstation and there is no trouble with request – H. Herzl Jun 14 '19 at 16:55
  • Hey, check you URL, maybe you have an invalid URL, it should be Photogallery/46, can you show your HttpPut attribute for action please? – H. Herzl Jun 14 '19 at 16:58

0 Answers0