![](/img/trans.png)
[英]Build query based on user input from Texbox and display it in another Textbox
[英]Display images based on Textbox input
我想基於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)
,但是如何將視圖中的模型作為參數傳遞?
我相信如果你通過這種方式創建img的src
<img src='@Url.Action("GetProducts", "Search", new{ id = i })' />
你不能看到參數字典的這個錯誤,在這種情況下你的圖像不會在瀏覽器上呈現。
我認為你從其他來源如按鈕或表單中擊中了這個方法。
實際上,我是這樣做的:
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不是理想的解決方案,但它對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.