子ajax表单重定向到URL http://www.AppName/Email/Email/AttachmentList,而不是仅刷新AttachmentList视图。
这是子表单(查看名称:附件列表)
@foreach (var attachment in Model.Attachments)
{
<li>
@using (Ajax.BeginForm("RemoveAttachment", "Email", new { area = "Email" }, new FSIAjaxOptions { UpdateTargetId = "uploadedFilesSection" }, new { @id = "fromEditRole" + attachment.IdString }))
{
@Html.HiddenFor(model => attachment.IdString)
<span><a `enter code here`href="@Url.Content("~/File/GetFile")?folder=EmailAttachments&file=@(attachment.AttachName)" target="_blank">@attachment.OriginalName</a></span>
<button type="submit" class="imageButton removeButton" title="Remove"></button>
}
</li>
}
这是控制器动作
[Authorize]
[HttpPost]
public ActionResult RemoveAttachment(FormCollection fm)
{
EmailViewModel model = SessionData.GetCurrentObject<EmailViewModel>();
if ((fm.AllKeys.Contains("attachment.IdString")) && !string.IsNullOrEmpty(fm["attachment.IdString"]))
{
var attachment = model.Attachments.Where(x => x.IdString == fm["attachment.IdString"]).FirstOrDefault();
if (attachment != null)
{
FileHelper.RemoveFile("EmailAttachments","",attachment.AttachName);
Directory.Delete(WebConfig.SharedFileContainer + @"\EmailAttachments\" + attachment.IdString, true);
model.Attachments.Remove(attachment);
}
SessionData.SaveCurrentObject<EmailViewModel>(model);
}
return View("AttachmentList", "Email", new { area = "Email" });
}
public ActionResult AttachmentList()
{
EmailViewModel email = SessionData.GetCurrentObject<EmailViewModel>();
if (Request.IsAjaxRequest())
{
return PartialView(email);
}
else
{
return View("AttachmentList", "~/Views/Shared/Wrapper.cshtml", email);
}
}
答案 0 :(得分:0)
确保引用了ajax js文件。