[英]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#. 我已经使用ASP.NET MVC 3 C#在我的SQL Server上成功上传了一个Image。
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? 但是当用户不更改图像时,存储在SQL Server中的值为空?
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
. 您正在直接更新表中的所有字段,因此将image字段设置为
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. 您可以检查是否确实在帖子中收到了图像,并且未在更新语句中将其设置为
NULL
,或者将图像上载提取为单独的操作。 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? ) 对于用户来说,为图像上传设置一个明确分开的UI可能会更加友好(否则他们可能会混淆:如果我编辑其他字段,我的图像会消失吗?)
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. 如果您未在编辑表单中提供默认值(默认值应来自先前存储的数据),则表单将以
NULL
数据提交。
In that case you will get NULL
during update. 在这种情况下,您将在更新期间获得
NULL
。
Hope this question will be a great help for you- 希望这个问题对你有很大的帮助 -
How update/edit uploaded files in asp.net mvc 5 without redundancy? 如何在没有冗余的情况下更新/编辑asp.net mvc 5中的上传文件?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.