2017-06-01 26 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、URL形式はどういう意味ですか? 'i'の値は、DataBaseのIDのパラメータとして渡します。 –

+0

'Model.producturl' if-conditionは' GetProducts'メソッドへのリクエストが利用できる唯一の部分ですか? 'int'は' long'に自動的にフィットするので、 '@Url.Action'は問題ではありません。デベロッパーコンソールの[ネットワーク]タブで要求された画像のURL形式も確認します。 –

答えて

0

実は、私はこのようにそれをやりました。

0

私はあなたがこの方法で

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

をIMGのSRCを作成している場合は、あなたのイメージがブラウザに表示されません、その場合には、PARAMTER辞書のこのエラーを見てはならないと考えています。

私はあなたがボタンやフォームのような他のソースからこのメソッドをヒットしていると思います。

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ない理想的なソリューションが、予想通り、それは私の作品:

関連する問題