我正在制作一个简单的表单,用户将在其中输入一些数据并选择要上传的文件。但我不能让它工作..由于某种原因,当我点击保存时,文件没有进入控制器。
这是一些代码。
@using (Ajax.BeginForm("Add", "Category", null, new AjaxOptions
{
UpdateTargetId = "upload-message",
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
OnSuccess = "uploadSuccess"
}, new { id = "AddCategoryForm", enctype = "multipart/form-data" }))
{
<div class="editorLabel">
@Html.LabelFor(m=>m.CategoryName)
</div>
<div class="editorText">
@Html.TextBoxFor(m=>m.CategoryName)
</div>
<div class="editorLabel">
@Html.LabelFor(m => m.Description)
</div>
<div class="editorText">
@Html.TextAreaFor(m => m.Description)
</div>
<div class="editorLabel">
@Html.LabelFor(m => m.IconPath)
</div>
<div class="editorText">
<input type="file" id="file" name="file" />
</div>
<div class="editorLabel">
@Html.LabelFor(m => m.IsActive)
</div>
<div class="editorText">
@Html.CheckBoxFor(m=>m.IsActive)
</div>
<p>
<input type="submit" id="submit" value="Save" />
</p>
}
控制器:
[HttpPost]
public ActionResult Add(HttpPostedFileBase file,CategoryViewModel model)
{
if (ModelState.IsValid)
{
System.IO.FileInfo info = new FileInfo(file.FileName);
string ext = info.Extension;
//other code
}
}
在控制器中,文件始终为空。我哪里做错了??