【发布时间】:2014-01-21 19:39:15
【问题描述】:
我正在尝试使用 MVC 4 ajax 表单添加图像,但它总是返回空值。我添加了我的模型、控制器和视图。
我的观点
@using (Ajax.BeginForm("Create", "Images", new AjaxOptions { HttpMethod = "Post", OnSuccess = "OnSuccess", OnFailure = "OnFailure", UpdateTargetId = "messageDiv" }, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="editor-field">
@Html.LabelFor(model => model.Image.Description, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(model => model.Image.Description)
</div>
@Html.TextBoxFor(model => model.FileImage, new { type = "file" })
@Html.ValidationMessageFor(model => model.FileImage)
</div>...
}
我的模型
public class ImageViewModel
{
public IList<Image> Images { get; set; }
public Image Image { get; set; }
public HttpPostedFileBase FileImage { get; set; }
}
我的控制器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ImageViewModel model, HttpPostedFileBase FileImage)
{
if (ModelState.IsValid)
{
var x = FileImage.FileName;
var imageUrl = Path.GetFileName(model.FileImage.FileName);
...
}
}
在此示例中,我无法获取文件名。它总是返回空值。你能帮我解决这个问题吗?我会很开心的。
【问题讨论】:
-
您无法使用 Ajax.BeginForm 上传文件。见stackoverflow.com/questions/8499748/…
标签: asp.net-mvc asp.net-ajax image-upload