我是MVC的初学者,正在尝试创建一个页面。我的数据库中有三个表。我正在使用实体框架和数据库优先的方法,我在我的Models文件夹中创建了这三个表作为类。我还创建了一个名为AllClasses的新类,并在这个新类中定义了这三个类中的每一个。
我正在尝试从tblCharacter表的CharacterImageURL列中获取3张图片。
当我运行我的代码时,程序运行时没有任何错误。那么问题出在哪里呢?
问题是我不能在我的视图页面上显示这三张照片。我将添加一些屏幕截图来向您显示更详细的问题。顺便说一下,我有图像网址的数据库,而不是像.jpg的图像。我的数据库像images/avatar/Chewbacca-icon.png一样喷发,我知道有类似的问题,但没有一个解决方案对我有效…
这是我的控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StarWars.Models;
namespace StarWars.Controllers
{
public class HomeController : Controller
{
StarWarsFunClubDBEntities myobject = new StarWarsFunClubDBEntities();
public ActionResult Index()
{
var myclass = new All();
myclass.Characters = myobject.tblCharacters.ToList();
return View(myclass);
}
public ActionResult SWHome()
{
var myclass = new AllClasses();
myclass.Avatars = myobject.tblAvatars.ToList();
myclass.Characters = myobject.tblCharacters.ToList();
myclass.Comments = myobject.tblComments.ToList();
return View(myclass);
}
}
}这是我的主页SWHome.cshtml
@model StarWars.Models.AllClasses
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>SWHome</title>
<link rel="stylesheet" type="text/css" href="~/Content/css.css" />
</head>
<body>
<div class="box">
<div class="header">
<div class="logo"> <img src="~/images/Star_Wars_Logo.png" alt="Alternate Text" /> </div>
<div class="sitename"><h4>Star Wars Fun Club</h4></div>
</div>
<div class="question"><h5>What do you think about the new characters of Star Wars: The Force Awakens(2015)?</h5></div>
<div class="photo">
@foreach (var item in Model.Characters)
{
<div class="innerphoto">
<img src="@item.CharacterImageURL" alt="" />
</div>
}
</div>
<div class="comment"></div>
<div class="addcomment"></div>
<div class="footer"></div>
</div>
</body>
</html>这些是我自动生成的类:
namespace StarWars.Models
{
using System;
using System.Collections.Generic;
public partial class tblAvatar
{
public int AvatarID { get; set; }
public string AvatarName { get; set; }
public string AvatarImageURL { get; set; }
}
public partial class tblComment
{
public int CommentID { get; set; }
public string Expression { get; set; }
public System.DateTime DateTime { get; set; }
public int AvatarID { get; set; }
}
public partial class tblCharacter
{
public int CharacterID { get; set; }
public string CharacterName { get; set; }
public string CharacterImageURL { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StarWars.Models;
using StarWars.Controllers;
using System.Data.Entity;
namespace StarWars.Models
{
public class AllClasses
{
public IEnumerable<tblAvatar> Avatars { get; set; }
public IEnumerable<tblCharacter> Characters { get; set; }
public IEnumerable<tblComment> Comments { get; set; }
}
}这是我想要显示照片的视图(SWHome):

此屏幕截图在底部显示了问题:

如您所见,在超文本标记语言<div class="innerphoto">中,可以看到图像源,但是我无法将它们显示在我的视图页面上。所以我认为问题不在数据库连接上。
这是我的解决方案资源管理器:

对于对以下内容感兴趣的人,我有了新的疑难解答:

发布于 2020-06-15 11:20:32
问题出在这里:
<img src="@item.CharacterImageURL" alt="" />无法直接从根目录(即wwwroot)访问镜像。
试试这个:
<img src= "@Url.Content(Model.YourImageLocationPropertyHere)" />当您希望解析站点上任何文件或资源的URL,并将相对路径传递给它时,可以使用Url.Content。
希望这能有所帮助。
https://stackoverflow.com/questions/62238433
复制相似问题