将图片从DB检索到MVC中的Details View

时间:2014-04-08 11:43:28

标签: asp.net entity-framework sql-server-2008 asp.net-mvc-4

我的数据库(VARBINARY(MAX))存储在我的数据库中,如下所示: enter image description here

我有一个控制器从DB获取字节并显示图片,这是控制器代码:

using SozlukRG.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SozlukRG.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            SozlukEntities db = new SozlukEntities();
            List<Kelime> kelime;
            kelime = null;
            return View(kelime);
        }
        public FileContentResult Show(int id)
        {
            SozlukEntities db = new SozlukEntities();
            Resim kelime = db.Resim.Find(id);
            var imagedata = kelime.Adi;

            return File(imagedata, "image/jpg");
        }

        [HttpPost]
        public ActionResult Index(string searchTerm)
        {
            SozlukEntities db = new SozlukEntities();
            List<Kelime> Kelime;
            if (string.IsNullOrEmpty(searchTerm))
            {
                Kelime = db.Kelime.ToList();
            }
            else
            {
                Kelime = db.Kelime
                    .Where(s => s.Kelime1.StartsWith(searchTerm)).ToList();
            }
            return View(Kelime);

        }
        public ActionResult Details(int id = 0)
        {
            SozlukEntities db = new SozlukEntities();
            KelimeTuru kelime = db.KelimeTuru.Find(id);

            if (kelime == null)
            {
                return HttpNotFound();
            }
            return View(kelime);
        }


        public JsonResult GetKelime(string term)
        {
            SozlukEntities db = new SozlukEntities();
            List<string> Kelime = db.Kelime.Where(s => s.Kelime1.StartsWith(term))
                .Select(x => x.Kelime1).ToList();
            return Json(Kelime, JsonRequestBehavior.AllowGet);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

这是我的详细信息视图,不显示图片:

@model SozlukRG.Models.KelimeTuru

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>


<fieldset>
    <p>Move the mouse pointer over this paragraph.</p>
    <legend>KelimeTuru</legend>
    <div class="div1" style="display: table; background-color: #b0c4de;">
        <div class="div1" style="display: table-row;">
            <div class="div1" style="display: table-cell; padding: 10px;">

                @Html.DisplayNameFor(model => model.KelimeId)
            </div>





            <div class="div1" style="display: table-cell; padding: 10px;">
                @Html.DisplayNameFor(model => model.VideoId)
            </div>


            <div class="div1" style="display: table-cell; padding: 10px;">
                @Html.DisplayNameFor(model => model.ResimId)
            </div>




            <div class="div1" style="display: table-cell; padding: 10px;">
                @Html.DisplayNameFor(model => model.Anlam)
            </div>
        </div>

            <div class="div1" style="display: table-row;">
                <div class="div1" style="display: table-cell; padding: 10px;">
                    @Html.DisplayFor(model => model.KelimeId)
                </div>
                <div class="div1" style="display: table-cell; padding: 10px;">
                    <video width="320" height="240" controls>
                        <source src="/Videos/b.mp4" type="video/mp4">

                    </video>
                </div>
                <div class="div1" style="display: table-cell; padding: 10px;">
                    <img src='<%= Url.Action( "show", "image", new { id = Model.Id } ) %>'>
                    **@* Here I am supposed to see the picture with the called id, but I can't*@**
                    </div>


                <div class="div1" style="display: table-cell; padding: 10px;">
                    @Html.DisplayFor(model => model.Anlam)
                </div>

        </div>
        </div>

</fieldset>
<p>

    @Html.ActionLink("Back to List", "Index")

</p>

如果我创建一个名为Resim的视图,并在地址栏中键入localhost:52278 / Home / show / 2,它就可以正常工作。 但是,当我打电话给详细信息视图时,我想查看图片: 本地主机:52278 /首页/信息/ 3 有人可以帮我在详情视图中显示我的照片吗? 提前谢谢..

2 个答案:

答案 0 :(得分:1)

当您在同一个控制器中执行Show操作时,您不需要控制器的名称。请告诉我们为什么会得到什么错误? Id不正确或您获得任何例外。

          Url.Action( "show", new { id = Model.Id } ) 

答案 1 :(得分:1)

最好定义一个渲染imageResult而不是actionResult的动作,并在你的html中将该动作的url放在img的src属性上(可能需要查询字符串)

    <img src='/Resim/Show/@Model.Id'>