我試圖顯示我以前在視圖中加載的圖像,但沒有成功。 圖片上傳,這是肯定的。到目前爲止,我已經寫了這樣的事情:保存/檢索圖像。 ASP.NET MVC
@if(Model.AttachedInformation.Count > 0)
{
<div id="gallery">
@foreach(var path in Model.AttachedInformation)
{
<img src="@path" alt="default_description" title="some_title" />
}
</div>
}
AttachedInformation
只是一個ICollection<String>
對象。
但這隻給我圖像的邊界。此外,我檢查了@path
變量,它確實保留了完整的文件路徑。
建議感激! 謝謝!
編輯:CONTROLLER
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Employee employee, IEnumerable<HttpPostedFileBase> files)
{
if(ModelState.IsValid)
{
var filepath = String.Empty;
foreach(var file in files)
{
if(null != file && file.ContentLength > 0)
{
filepath = Path.Combine(
HttpContext.Server.MapPath("~/Content/Images/Uploads"),
Path.GetFileName(file.FileName)
);
file.SaveAs(filepath);
employee.AttachedInformation.Add(filepath);
}
}
this.repository.Add(employee);
return Redirect("/");
}
else
{
return View(employee);
}
}
模型
AbstractEntity
保持Version
和ID
性能。
[Serializable]
public class Employee : AbstractEntity<Employee>, IAggregateRoot
{
public Employee()
{
this.AttachedInformation = new HashSet<String>();
}
public virtual String FirstName { get; set; }
public virtual String MiddleName { get; set; }
public virtual String LastName { get; set; }
public virtual String SSN { get; set; }
public virtual String EmployeeNumber { get; set; }
public virtual String TradeUnionNumber { get; set; }
public virtual String Department { get; set; }
public virtual String Post { get; set; }
public virtual DateTime DateOfHire { get; set; }
public virtual DateTime DateOfBirth { get; set; }
public virtual ICollection<String> AttachedInformation { get; set; }
}
VIEW
視圖包含多個input
s的名字files
像:
<input type="file" name="files" id="additional" class=" " accept="image/jpeg,image/png,image/gif" />
RAW HTML
<div id="gallery">
<img src="D:\Programming.Projects\BOA\branches\BOA.PresentationLayer\Content\Images\Uploads\Desert.jpg" alt="default_description" title="some_title" />
<img src="D:\Programming.Projects\BOA\branches\BOA.PresentationLayer\Content\Images\Uploads\Hydrangeas.jpg" alt="default_description" title="some_title" />
<img src="D:\Programming.Projects\BOA\branches\BOA.PresentationLayer\Content\Images\Uploads\Jellyfish.jpg" alt="default_description" title="some_title" />
<img src="D:\Programming.Projects\BOA\branches\BOA.PresentationLayer\Content\Images\Uploads\Koala.jpg" alt="default_description" title="some_title" />
</div>
BOUNTY
我需要的是得到一個說明的例子(工程)。謝謝!
「真的保留完整的文件路徑」在'file:///'路徑中嗎?或者路徑實際上在瀏覽器中工作? – EAMann 2012-03-03 00:42:38
請提供該功能的HTML輸出...(或者當您轉到頁面時可能會出現一些錯誤?) – debracey 2012-03-03 00:44:09
您是否可以添加一個控制器來顯示如何填充模型? – 2012-03-03 00:45:16