更新在ASP.NET MVC 3中上传到SQL Server的图像 [英] Update an Image uploaded to SQL Server in ASP.NET MVC 3

查看:66
本文介绍了更新在ASP.NET MVC 3中上传到SQL Server的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用ASP.NET MVC 3 C#在SQL Server上成功上传了图像.

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.

但是当用户不更改图像时,SQL Server中存储的值是否为空?

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

如何避免这种情况.

,如果未在编辑视图中选择/更新图像,则不应将先前存储在数据库中的图像更新为空值.

i.e, 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.

推荐答案

问题是,当用户不更改图像时,帖子中将不包含图像数据.您将直截了当地更新表格中的所有字段,因此将图像字段设置为NULL.

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.

您可以检查帖子中是否确实收到了图像,并且没有在update语句中将其设置为NULL,也可以将图像上传提取为单独的操作.对于用户而言,使用清晰分开的UI上传图片可能会更加友好(否则,他们可能会感到困惑:如果我编辑其他字段,图片会消失吗?)

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? )

这篇关于更新在ASP.NET MVC 3中上传到SQL Server的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆