我已經開始與MVC4 + EF工作,面臨着圖片上傳的問題: 我要保存的圖像路徑數據庫,但實際文件~/App_Data/
傳遞多個對象,控制器MVC
我需要將「HttpPostedFileBase
」和我的自定義「Image
」對象傳遞給「Create
」控制器。我創建了一個類「ImageUpload
」來傳遞這兩個對象。
「Image
」 類:
public class Image
{
public int Id { get; set; }
public string Name { get; set; }
public string Path { get; set; }
public int DepartmentId { get; set; }
}
「ImageUpload」 類:
public class ImageUpload
{
public Image image { get; set; }
public HttpPostedFileBase file { get; set; }
}
查看:
@model MvcBannikov.Models.ImageUpload
@using (Html.BeginForm("Create", "Image", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>New Photo</legend>
<div class="editor-label">
<p>Name:</p>
</div>
<div class="editor-field">
@Html.EditorFor(model => model.image.Name)
@Html.ValidationMessageFor(model => model.image.Name)
</div>
<div class="editor-label">
<p>Path:</p>
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.file, new { type = "file" })
@Html.ValidationMessageFor(model => model.file)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
「Create
」 的方法:
[HttpPost]
public ActionResult Create(ImageUpload image)
{
......................
}
的問題是,ImageUpload
對象是NULL
:(((
我之所以需要一個文件和圖像類是Image類有一個部門ID - 我需要id
。
也許你知道另一個解決方案如何將文件一起傳遞給DepartmentId
?
謝謝你的幫助
謝謝你的回覆。我的ImageUpload類是作爲某種ViewModel創建的,我想我理解了這個基本思想,但它不適用於我創建的代碼。我爲這個「Create」動作創建了一個強類型的視圖,但它仍然是NULL。也許在我的觀點問題? – user1274184