我正在尝试将 HttpPostedFileBase 对象的 IList 和“发布”对象传递给操作。我收到 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>
}