简体   繁体   English

更新在ASP.NET MVC 3中上载到SQL Server的映像

[英]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.

相关问题 在ASP.NET MVC中使用Java脚本将上传的图像设置为背景 - Setting Uploaded Image as Background using Javascript in ASP.NET MVC Asp.Net MVC在“编辑获取”中编辑上传的图像(HTTPPostedFileBase) - Asp.Net MVC Edit an uploaded image (HTTPPostedFileBase) in Edit Get 在ASP.NET MVC中使用相对路径将上传的图像保存在数据库中 - Saving uploaded image in database with relative path in asp.net mvc 上载到ASP.NET中的服务器时,浏览器中不显示图像 - Image is not displayed in browser when uploaded to server in ASP.NET ASP.NET:调整上传到服务器的图像的大小(高度和宽度) - ASP.NET: resize (height and width) an image uploaded to server ASP.NET Store上传的文件sql server表 - ASP.NET Store uploaded file sql server table Asp.net Mvc 6组合浏览图像并将图像大小图像名称添加到sql server中 - Asp.net Mvc 6 Combine browse image and add image size image name into sql server 在asp.net mvc 2中上传文件的文件路径 - filepath of uploaded file in asp.net mvc 2 如何在MVC asp.net core +在SQL Server上更新中上传文件? - How to upload files in MVC asp.net core + update on sql server? ASP.NET MVC 4:对 controller 进行 Ajax 调用以更新 Z9778840A0100CB30C982228B7 数据库时出错 - ASP.NET MVC 4: error when making Ajax call to controller to update SQL Server database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM