HttpPostedFileBase オブジェクトの IList と「Post」オブジェクトをアクションに渡そうとしています。Post オブジェクトは問題なく受け取りますが、IList は常に空です。
以下のコードを参照してください...
コントローラーのアクション:
[HttpPost]
public ActionResult Create(Post post, IList<HttpPostedFileBase> attachments)
{
if (ModelState.IsValid)
{
var attachmentCounter = attachments.Count;
post.SubmissionDateTime = DateTime.Now;
db.Posts.AddObject(post);
db.SaveChanges();
return RedirectToAction("Index", new { category = post.Category });
}
return View(post);
}
意見:
@using (Html.BeginForm("Create", "Posts", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Post</legend>
@Html.HiddenFor(model => model.Category)
@Html.HiddenFor(model => model.Author)
<div class="editor-label">
<label>Photo Attachments:
<span style="color:#666; font-size:12px; font-weight:normal;">(Optional)</span>
</label>
</div>
<div class="editor-field">
@Html.Raw(FileUpload.GetHtml(
name: "attachments",
initialNumberOfFiles: 1,
allowMoreFilesToBeAdded: true,
includeFormTag: false,
addText: "Add another photo...",
uploadText: "").ToString().Replace("<input value\"\" type=\"submit\" />", ""))
</div>
<br />
<p>
<input type="submit" value="Create" class="createButton" style="font-weight:normal;" /> |
@Html.ActionLink("Back to List", "Index", null, new { category = Model.Category }, null)
</p>
</fieldset>
}