【发布时间】:2014-01-03 17:53:49
【问题描述】:
我有 2 个文件上传,用于在从用户个人资料页面调用的部分视图中编辑用户个人资料。问题是动作方法中的HttpPostedFileBase参数总是null。
当我在个人资料页面之外调用此部分视图并且没有布局时,文件上传已成功完成并将文件发送到操作方法。
这是我在控制器中的操作方法:
[AcceptVerbs(HttpVerbs.Post)]
[Authorize(Roles = "User")]
public ActionResult EditProfile(ProfileGeneralDescription editedModel,
HttpPostedFileBase imageFile,
HttpPostedFileBase coverFile)
{
//Some code here...
}
这是我的部分观点cshtml代码:
@model Website.Models.ViewModel.Profile
@using (Ajax.BeginForm("EditProfile", "Profile", new { postOwnerUser = User.Identity.Name }, new AjaxOptions()
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "GeneralSection"
}, new { enctype = "multipart/form-data" }))
{
<div>
<button type="submit" name="Save" class="btn btn-default btn-xs">Save Changes</button>
@Ajax.ActionLink("Cancel", "ProfileDescription", "Profile",
new {username = Model.Username, type = "Show"},
new AjaxOptions()
{
HttpMethod = "GET",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "GeneralSection"
},
new {@class = "btn btn-default btn-xs"})
</div>
<input type="hidden" name="username" id="username" value="@Model.Username"/>
<fieldset>
<legend>Edit Photos</legend>
<div>
Select profile picture:
<input id="imageFile" type="file" name="imageFile" accept="image/png, image/jpeg" />
@Html.CheckBoxFor(modelItem => modelItem.DefaultCover)<span>Remove profile photo</span>
</div>
<div>
Select cover picture:
<input id="coverFile" type="file" name="coverFile" accept="image/png, image/jpeg" />
@Html.CheckBoxFor(modelItem => modelItem.DefaultCover)<span>RemoveCover</span>
</div>
</fieldset>
}
我的错误在哪里?
提前致谢。
【问题讨论】:
-
你需要[HttpPost]属性吗?
-
我忘记复制这些代码了。请查看我的更新。
-
你能给出代码显示当局部视图不工作时你是如何调用它的吗?
-
这是我的操作链接:
@Ajax.ActionLink("Edit Profile", "ProfileGeneralDescription", "Profile", new { username = Model.Username, type = "Edit" }, new AjaxOptions() { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "GeneralSection" }, new { @class = "btn btn-default btn-xs" } ) -
我找到了这篇文章:mvc3 file upload using ajax form - Request.Files empty,最佳答案是 Ajax 表单不支持文件上传。我该怎么做才能编辑用户资料?
标签: c# asp.net-mvc asp.net-mvc-4 file-upload ajaxform