我有 HTML :
<% using (Html.BeginForm("Upload", "BulkUpload", FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
<%: Html.TextBoxFor(model => model.UploadFile, new { @type = "file" }) %>
<%: Html.ValidationMessageFor(model => model.UploadFile, "*") %>
<%= Html.Button("submit", "Upload", null) %>
<% } %>
这个模型:
public class BulkUploadVM
{
[DisplayName("File to import")]
public HttpPostedFileBase UploadFile { get; set; }
}
这个控制器:
[HttpPost]
public ActionResult Upload(BulkUploadVM model)
{
if (ModelState.IsValid)
{
var stream = model.UploadFile.InputStream;
// Process stream ...
}
return View("Index", model);
}
上传文件正确发布到Controller,但是当Controller返回带有模型的View(具有有效的UploadFile)时,页面上的文件路径是空的?
这是默认行为吗?或者我错过了什么?
答案 0 :(得分:0)
<% using (Html.BeginForm("Upload", "Home", new { BulkUploadVM = Model },
FormMethod.Post, new { enctype = "multipart/form-data" })) { %>
<%= Html.TextBoxFor(model => model.UploadFile, new { @type = "file" }) %>
<%= Html.ValidationMessageFor(model => model.UploadFile, "*") %>
<input type="submit" value="Finish" />
<% } %>
设置routeValues对象
new { BulkUploadVM = Model }
答案 1 :(得分:0)