论坛上有一些类似的话题,但没有任何好的答案。第一个问题是,当使用模式弹出窗口时,是否可以将 httppostedfilebase 传递给控制器?当我使用常规模型和视图时,一切都运行良好,但是一旦我将此视图设置为部分视图,那么除了文件之外的所有内容都会传递给控制器。文件正在获取空值。
modalpopup 正在执行的列表视图:
<button style="cursor:pointer" type="submit" class="fluent-big-button" onclick="ShowModal('@Url.Action("Create", "Device")')" >
模态弹出视图:
@using (Html.BeginForm("Create", "Device", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="modal-body">
<div class="form-horizontal">
<fieldset>
<legend>@Resources.Insert_New_Device</legend>
<div class="grid" style="margin: 0px auto">
<div class="row" style="margin: 0px auto">
<div class="span10">
<div class="row" style="margin: 0px auto">
<div style="white-space:nowrap" class="span2 readable-text"><label>@Resources.Device_No:</label> </div>
<div class="span3">
<div class="input-control text" data-role="input-control">
@Html.TextBoxFor(m => Model.No, new { required = "required", autofocus = "autofocus", @tabindex="1"})
@Html.ValidationMessageFor(m => Model.No)
<button tabindex="-1" class="btn-clear" type="button"></button>
</div>
</div>
<div style="white-space:nowrap" class="span2 readable-text"><label>Tootja:</label> </div>
<div class="span3">
<div class="input-control text" data-role="input-control">
@Html.TextBoxFor(m => Model.Manufacturer_Name, new { autofocus = "autofocus", @tabindex="2"})
@Html.ValidationMessageFor(m => Model.Manufacturer_Name)
<button tabindex="-1" class="btn-clear" type="button"></button>
</div>
</div>
</div>
<div class="row" style="margin: 0px auto">
<div style="white-space:nowrap" class="span2 readable-text"><label>Pilt:</label> </div>
<div class="span3">
<div class="fallback">
<input type="file" name="DeviceImage" id="DeviceImage" />
</div>
</div>
</div>
</div>
<div class="row" style="margin: 0px auto">
<div class="span10">@Html.ValidationMessage("CustomError")</div>
</div>
</div>
</div>
</fieldset>
</div>
</div>
<div class="modal-footer">
<div class="grid" style="margin: 0px auto">
<div class="row" style="margin: 0px auto">
<div class="span4"></div>
<div class="span3">
<button class="button large span3" data-dismiss="modal">Cancel</button>
</div>
<div class="span3">
<input class="large span3 success" type="submit" value="Ok" />
</div>
</div>
</div>
</div>
}
控制器:
public ActionResult Create()
{
ViewBag.exsistingDevices = GetExsistingDevices();
return PartialView("_Create");
}
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin, Super")]
public async Task<ActionResult> Create(Device device, HttpPostedFileBase DeviceImage)
{
try
{
if (ModelState.IsValid)
{
SPDBContext db = new SPDBContext();
device.Last_Action_Date = DateTime.Now;
// Insert image
if (DeviceImage != null)
{
if (DeviceImage.ContentLength > (2 * 1024 * 1024) && DeviceImage.ContentLength < 1)
{
ModelState.AddModelError("CustomError", "File size must be less than 2 MB");
}
var contentType = DeviceImage.ContentType;
device.Image = new byte[DeviceImage.ContentLength];
DeviceImage.InputStream.Read(device.Image, 0, DeviceImage.ContentLength);
}
db.Devices.Add(device);
await db.SaveChangesAsync();
return Json(new { success = true });
}
}
catch (Exception e)
{
ModelState.AddModelError("CustomError", String.Format("{0}: {1}",Resources.Resources.Insert_Failed, e.Message));
}
return PartialView("_Create", device);
}
模型:
[NotMapped]
public HttpPostedFileBase DeviceImage { get; set; }
public byte[] Image { get; set; }