Last post Jan 15, 2018 05:59 AM by Edward Z
Jan 14, 2018 04:28 PM|sadin|LINK
I have a table in db:
Id, title, imageurl
In add view I get IFormFile for image url and get the relative path and save image url it is ok but in edit view I want to edit this record and I want to save new image url if user select a new image or save old imageurl if user leave image blank.
If I use db.ImagesTable.Update(myviewmodel);
If user does not select a new image it will fill the imageurl with null value.
Can anyone help me to know how can I set this field required in add view and optional in edit view that if user does not select image in edit view automatically fill this field with old value?
Jan 15, 2018 05:59 AM|Edward Z|LINK
>> If I use db.ImagesTable.Update(myviewmodel); If user does not select a new image it will fill the imageurl with null value.
To avoid this issue, you could retrieve record in db by model.Id, and update the required properties if you did not select any image.
Here is a simple code.
if (forum.Image == null)
var model = await _context.Forum.SingleOrDefaultAsync(m => m.Id == forum.Id);
//update the required properties
forum.Image = model.Image;