MVC - 我在显示数据库中的图像方面遇到了麻烦

时间:2022-02-25 22:25:38

I need to display image from database in my site but it does not want.

我需要在我的网站中显示来自数据库的图像,但它不需要。

I have such result:My web-site

我有这样的结果:我的网站

I have tried all of the other * help topics on here already and none of them are working for me.

我已经在这里尝试了所有其他*帮助主题,但没有一个对我有用。

I am following the directions in "Pro ASP.NET MVC Framework" by Steven Sanderson (pages 256-262) and it is just not working.

我遵循Steven Sanderson的“Pro ASP.NET MVC框架”中的指示(第256-262页)并且它不起作用。

My class:

我的课:

[HttpPost]
    public ActionResult Edit(Product product, HttpPostedFileBase image)
    {
        if (ModelState.IsValid)
        {
            if (image != null)
            {
                product.ImageMimeType = image.ContentType;
                product.ImageData = new byte[image.ContentLength];
                image.InputStream.Read(product.ImageData,0,image.ContentLength);
            }
            repository.SaveProduct(product);
            TempData["message"] = string.Format("{0} has been saved", product.Name);
            return RedirectToAction("Index");
        }      
            return View(product);    
    }

My action method:

我的行动方法:

public FileContentResult GetImage(int productId)
    {
        Product prod = repository.Products.FirstOrDefault(p => p.ProductID == productId);
        if (prod != null)
        {
            return File(prod.ImageData, prod.ImageMimeType);
        }
        else
        {
            return null;
        }
    }

And my view:

我的观点是:

View:

视图:

@model SportsStore.WebUI.Domain.Entities.Product

@using(Html.BeginForm("Edit","Admin",FormMethod.Post, new { enctype = "multipart/form-data" }))

{

  @Html.EditorForModel()

    <div class="editor-label">Image</div>
    <div class="editor-field">
    @if (Model.ImageData == null)
    {
        @:None 
    }
    else
    {
        <img width="150" height="150" scr="@Url.Action("GetImage", "Product", new {Model.ProductID })"/>
    }
      <div>Upload new image: <input type="file" name="Image" /></div>
</div>
<input type="submit" value="save"/>
@Html.ActionLink("Cancle and return to List","Index") 

}

}

1 个解决方案

#1


1  

You have the attribute scr instead of src.

你有属性scr而不是src。

#1


1  

You have the attribute scr instead of src.

你有属性scr而不是src。