Khi tôi nhấn gửi, thông số file
là không.Mô hình là null khi biểu mẫu được gửi
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; }
}
Đây là 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>
Shiat! Tôi không tin nó sẽ hoạt động nhưng nó đã làm. Tại sao thực sự điều này lại xảy ra? – Shimmy
@Shimmy Đó là đặt tên xung đột và liên kết cố gắng liên kết thuộc tính 'File' của bạn với đối tượng' FileViewModel' với tên 'tệp', đó là lý do tại sao bạn nhận được' null'. Tên POST có phân biệt chữ hoa chữ thường. – webdeveloper
Bạn là người đàn ông tốt! Nắm bắt tốt! – Shimmy