2017-06-01 65 views
1

我想基于TextBox输入在我的视图中显示大量图像。基于文本框输入显示图像

我有这个Controller

public ActionResult GetProducts(long id, Search obj) 
{ 
    GetSearchByDropdowns(); 
    using (ThBEntities1 db = new ThBEntities1()) 
    { 
     if (id == null) { 
      return View("Index"); 
     } 

     byte[] image = db.Searches.Where(x => x.SearchTextBox == x.Product).SingleOrDefault().producturl; 
     var stream = new MemoryStream(image.ToArray()); 
     return new FileStreamResult(stream, "image/jpeg"); 
    } 
    return View("~/Views/Search/Index.cshtml", obj); 
} 

在我看来

if (Model.producturl != null) 
{ 
    for (int i = 0; i < 6; i++)--I only have 6 records 
    { 
     <img src='@Url.Action("GetProducts", "Search", new{ id = i })' /> 
    } 
} 

而且我的模型

public partial class Search 
{ 
    public long ID { get; set; } 
    public string Product { get; set; } 
    public byte[] producturl { get; set; } 
} 

我收到此错误:

The parameters dictionary contains a null entry for parameter 'id' of 
non-nullable type 'System.Int64' for method 
'System.Web.Mvc.ActionResult GetProducts(Int64, ThunderBird.Search)' 
in 'ThunderBird.Controllers.SearchController'. An optional parameter 
must be a reference type, a nullable type, or be declared as an 
optional parameter. 

我知道它来自GetProducts(long id, Search obj),但是如何将视图中的模型作为参数传递?

+0

您是否检查浏览器中呈现的URL格式是什么?它对'i'具有约束力吗? –

+0

@SivaGopal,你的网址格式是什么意思?来自'i'的值我将它作为DataBase中ID的参数传递。 –

+0

是否只有'Model.producturl'条件是GetProducts方法可用的唯一部分?由于'int'可能自动适合'long',因此'@ Url.Action'不应该成为问题。还请在开发者控制台的“网络”标签中查看请求的图片网址 –

回答

0

其实,我这样做是这样的:

Controller 
     public ActionResult GetProducts(Search obj) 
      { 
       GetSearchByDropdowns(); 

       Search c = null; 
       SqlConnection conn = new SqlConnection(); 
       conn.ConnectionString = "Data Source=DESKTOP-QR66UQL;Initial Catalog=ThB;Integrated Security=True"; 
       conn.Open(); 
       using (conn) 
       { 
        SqlCommand cmd = new SqlCommand("Select ProductImage from Search where Product='" + obj.SearchTextBox + "'"); 
        cmd.Connection = conn; 
        SqlDataReader reader = cmd.ExecuteReader(); 
        while (reader.Read()) 
        { 
         c = new Search(); 
         c.ImagePath = reader["ProductImage"].ToString(); 
         obj.S.Add(c); 
        } 
        return View("~/Views/Search/Index.cshtml", obj); 
       } 
      } 

View 
if (Model.S != null) 
    { 
     foreach (var item in Model.S) 
     { 
      <img src='@Url.Content(Model.ImagePath)' alt="Image" /> 
     } 
    } 

Mybe不是一个理想的解决方案,但预期它为我工作。

0

我相信,如果你是这样

<img src='@Url.Action("GetProducts", "Search", new{ id = i })' /> 

你不能看到放慢参数字典的这个错误产生的IMG SRC,在你的形象将不会在浏览器中渲染的情况下。

我认为你是从按钮或窗体的其他来源打这个方法。