【问题标题】:Image not showing in the asp.net MVC list View图像未显示在 asp.net MVC 列表视图中
【发布时间】:2020-03-20 08:27:06
【问题描述】:

如何在列表视图中显示每个项目的图像?我的数据库中有ImageNameImagePathnvarchar(MAX)。我可以通过在视图模型中使用HttpPostedFileBase 很好地保存。但是在列表中检索它时,没有图像显示!! foreach循环还有其他方法吗,还是我语法错误?

我该如何解决这个问题?

public partial class Course
{
    public int CourseId { get; set; }
    public string CourseTitle { get; set; }
    public string ImageName { get; set; }
    public Nullable<double> Price { get; set; }
    public string Description { get; set; }
    public string ImagePath { get; set; }
}

public class CourseViewModel
{
    public string CourseTitle { get; set; }
    public HttpPostedFileBase ImageFile { get; set; } 
    public Nullable<double> Price { get; set; }
    public string Description { get; set; }
    public string ImagePath { get; set; }
}

public ActionResult Index()
{
    var courses = db.Courses.ToList();
    return View(courses);
}

public ActionResult Create(FormCollection collection, CourseViewModel courseVM, HttpPostedFileBase ImageFile)
{
        try
        {
            if (!ModelState.IsValid)
            {
                return View(courseVM);
            }
            else
            {
                var image = ImageFile;

                if (image!=null && image.ContentLength>0)
                {
                    Course course = new Course();

                    var imageName = Path.GetFileName(image.FileName);
                    var imageExtention = Path.GetExtension(imageName);
                    imageName = imageName + DateTime.Now.ToString("yymmssfff") + imageExtention;
                    course.ImageName = imageName;

                    var imagePath = "~/App_Data/Images/" + imageName;
                    var path = Path.Combine(Server.MapPath("~/App_Data/Images/"), imageName);
                    image.SaveAs(path);

                    course.ImagePath = path;
                    course.CourseTitle = courseVM.CourseTitle;
                    course.Description = courseVM.Description;
                    course.Price = courseVM.Price;

                    db.Courses.Add(course);
                    db.SaveChanges();

                    return RedirectToAction("Index");
                }
                else
                {
                    return View(courseVM);
                }
            }
        }
        catch
        {
            return View();
        }
}        

这是我的图像列列表视图:

@model IEnumerable<MyProject.Models.Course>

@foreach (var item in Model) {
    <tr>

        <td>
            <img src="@Url.Content(item.ImagePath)" width="100" height="100" alt="Image" />

        </td>
    </tr>

【问题讨论】:

    标签: c# sql asp.net-mvc razor


    【解决方案1】:

    我首先要看的是正在写入锚标记的src="" 属性的文本。在 Chrome 中,右键单击图像应位于的位置,然后单击“检查”。

    我怀疑这里的字符串格式不正确。 HttpPostedFileBase.FileName 属性的文档指出,这将返回“客户端上的文件名,包括目录路径。”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 2015-11-13
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      相关资源
      最近更新 更多