2017-09-25 19 views
0

在我的代碼的整體形象已被查看,但我只需要特定的ID值圖象顯示的..如何使用MVC查看關於特定ID值的單個圖像?

<div class="photo"> 
    @foreach (var imgPath in Directory.GetFiles(Server.MapPath("~/ComapnyLogo"), "*.png")) 
    { 
     var img = new FileInfo(imgPath); 
     <img src="@Url.Content(String.Format("~/ComapnyLogo/{0}", img.Name))" /> 
    } 
</div> 

隱藏標識

<input type="hidden" value="@ViewBag.hdnCompany" id="hdnCompany" /> 

Image

public ActionResult ViewCompany() 
     { 
      var data = dp.Company.SqlQuery("Select * from CompanyRegistration").ToList(); 
      return View(data); 
     } 
+0

如果你只想要一個圖像,爲什麼你遍歷所有這些?隱藏字段中存儲了什麼值?它只是Id嗎?你能用_just_這個Id來製作一個圖像路徑嗎? – maccettura

+0

@maccettura我使用會話ID保存了圖像 –

回答

0

小錯誤,我清除問題

<img src="@Url.Content(String.Format("~/ComapnyLogo/" + MainModel.CompanyID + ".png"))" /> 

在此代碼工作正常在我身邊..