更改文件名和显示而不更改原始名称

时间:2015-01-27 05:14:48

标签: c# asp.net asp.net-mvc-5

我是asp.net的新手,最近正在开展一个项目。我已经使用更改的名称在数据库中保存了文件名。它类似于

  

682c3494-2c16-4de6-BC30-c4c4ab624d7c_Penguins.jpg

。当我发送它以查看它时,它显示整个名称。我需要做的是在" _"之前删除字符串。并显示它

  

Penguins.jpg

但我不知道该怎么做。

在控制器中我读了这样的数据库记录

var attachment = db.Attachment.Where(z => z.AttachmentID == id).ToList();

在我的视图页面中,我只是像这样显示它

@item.DocumentName

我不知道如何从文件名中删除上一部分并将其发送到视图。 有没有人可以帮助我,我非常感激

2 个答案:

答案 0 :(得分:3)

使用String.Substring方法删除682c3494-2c16-4de6-bc30-c4c4ab624d7c_

例如:

@item.DocumentName.Substring(37) // 37 is the length of string from first char to _

答案 1 :(得分:2)

使用IndexOf()Substring()的解决方案。如果您的文件名具有相同的文件名格式A character seqience + _ + actual file name

,则可以使用此方法
String fname = "682c3494-2c16-4de6-bc30-c4c4ab624d7c_Penguins.jpg"; // item.DocumentName can be used here

int index = fname.IndexOf("_"); // getting index of "_" 

String extracted = fname.Substring(index+1); // Extracting actual file name