当我点击提交时,file
参数为空。
public ActionResult Create()
{
return View(new FileViewModel());
}
[HttpPost]
[InitializeBlobHelper]
public ActionResult Create(FileViewModel file)
{
if (ModelState.IsValid)
{
//upload file
}
else
return View(file);
}
public class FileViewModel
{
internal const string UploadingUserNameKey = "UserName";
internal const string FileNameKey = "FileName";
internal const string Folder = "files";
private readonly Guid guid = Guid.NewGuid();
public string FileName
{
get
{
if (File == null)
return null;
var folder = Folder;
return string.Format("{0}/{1}{2}", folder, guid, Path.GetExtension(File.FileName)).ToLowerInvariant();
}
}
[RequiredValue]
public HttpPostedFileBase File { get; set; }
}
这是cshtml:
@model MyProject.Controllers.Admin.FileViewModel
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_BackOfficeLayout.cshtml";
}
@using (Html.BeginForm("Create", "Files", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<fieldset>
<legend>Create</legend>
<div class="editor-label">
@Html.LabelFor(model => model.File)
</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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
答案 0 :(得分:50)
它命名冲突和绑定器尝试将File属性绑定到带有文件名的FileViewModel对象,这就是为什么你得到null。 POST名称不区分大小写。
变化:
public ActionResult Create(FileViewModel file)
要:
public ActionResult Create(FileViewModel model)
或任何其他名称
答案 1 :(得分:1)
这也解决了我的问题。这是我使用的名称与模型相似,类似于我分配的模型的变量。一旦我整理了字段名称,所有工作都按预期工作。
当然,这个错误并没有帮助指出这一点。