【发布时间】:2017-03-31 10:10:28
【问题描述】:
我通过 Ajax 处理了部分重新加载页面。 这就是我所拥有的:
控制器:
public class EmailController : Controller
{
//
// GET: /Email/
[Authorize]
public ActionResult Index(string id)
{
//.. Some Code here to get my Email List
return View(emails);
}
[Authorize]
public PartialViewResult EmailPartial(string id = "1,2")
{
//.. Some Code here to get my Email List with the new filter
return PartialViewResult(emails);
}
}
主视图:
@model IEnumerable<namespace.Emailgesendetview>
@{
ViewBag.Title = "Index";
AjaxOptions ajaxOpts = new AjaxOptions
{
UpdateTargetId = "emailBody"
};
Layout = "~/Views/Shared/_Default.cshtml";
}
// .... uninteresting html code
<div id="emailBody">
@Html.Action("EmailPartial", new { selectedRole = Model })
</div>
// .... uninteresting html code
// Here comes my action link:
<li>
@Ajax.ActionLink("martin",
"EmailPartial",
"Email",
new { id = "3"},
new AjaxOptions()
{
HttpMethod = "GET",
AllowCache = false,
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "emailBody"
})
</li>
局部视图:
@model IEnumerable<namespace.Emailgesendetview>
<div class="mail-body">
<ul class="mail-list">
@{
foreach (var userMail in Model)
{
//... Code vor showing the models
}
}
</ul>
</div>
如果我现在单击链接,他会启动功能:EmailPartial,但他没有添加(或替换我都尝试过的)内容。他单独打开 EmailPartial 视图?!
在这篇文章中: How to render partial view in MVC5 via ajax call to a controller and return HTML Partial 函数不是 PartialViewResult 它是 ActionResult,但我已经尝试过了。
如果你能告诉我我的错误或错误,那就太好了。
非常感谢,祝你有美好的一天:)
【问题讨论】:
-
为什么要在函数参数中赋值? EmailPartial(string id = "1,2")
-
听起来你做了一个标准的获取,你的脚本中可能缺少 jquery.unobtrusive.js。这就是在 Mvc 中连接 Ajax 函数的原因
-
确保我的列表有一个值,如果页面加载时没有 ajax 调用,我想获取索引为 1 和 2 的邮件:)
-
因为您没有在视图或布局中包含
jquery-unobtrusive-ajax.js,所以它会进行正常重定向。
标签: c# .net ajax asp.net-mvc