简体   繁体   中英

Update an Image uploaded to SQL Server in ASP.NET MVC 3

I have successfully uploaded an Image on my SQL Server using ASP.NET MVC 3 C#.

Also I have managed to successfully displayed/downloaded that image in the Index view.

However, the problem occurs when in the Edit view of the data,

If the Image has been changed by selecting the file in the view , the new image stores in the DB and everything is okay.

But when the user does not change the image, the value stored in the SQL Server is a null?

How to avoid this.

ie , if the image is not selected/updated in the edit view, the previously stored image in the database should not be updated to a null value.

The problem is that when the user doesn't change the image the post doesn't contain the image data. You are bluntly updating all the fields in your table and therefore set image field to NULL .

You can either check if you've actually received an image in the post and do not set it to NULL in update statement, or extract the image upload to separate action. It may be even more friendly for the user to have a clearly separated UI for image upload (otherwise they could be confused: if I edit some other fields, will my image disappear? )

For the edit page, you should have add default post data which was previously stored.

If you are not giving default value in the edit form (default value should come from previously stored data), the form will be submitted with NULL data.

In that case you will get NULL during update.


Hope this question will be a great help for you-

How update/edit uploaded files in asp.net mvc 5 without redundancy?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM