I have used PartialView but it renders the whole view after success full ajax post
This is my view
my ul which i want to update
<ul id="ULdinnerlist">
@* my foreach loop*@
@foreach (var item in Model)
{
<li> Title : @Html.DisplayFor(modelItem => item.Title)<br />
EventDate : @Html.Encode(item.EventDate.ToShortDateString()+ " @ " + item.EventDate.ToShortTimeString())
<p>Description : @Html.DisplayFor(modelItem => item.Description)</p><br />
HostedBy : <b>@Html.DisplayFor(modelItem => item.HostedBy)</b> (@item.ContactPhone)<br />
</li>
}
</ul>
我的ajax路线链接 @ Ajax.RouteLink(“&gt;&gt;&gt;”,新的{page = Model.PageIndex + 1},新的AjaxOptions {UpdateTargetId =“ULdinnerlist”})
ON controller: my action for reindering
public ActionResult Index(int? Page)
{
int PageSize = 5;
IQueryable<Dinner> dines = from dine in db.Dinners orderby dine.EventDate select dine;
var paginatedDinners = new PagingList<Dinner>(dines, Page ?? 0, PageSize);
checking ajax post and rendering accordingly
if (Request.IsAjaxRequest())
{
return PartialView(paginatedDinners);
}
return View(paginatedDinners);
}
问题是当我进行分页时它呈现整个视图。 感谢
答案 0 :(得分:0)
要返回不同的信息以响应您的AJAX请求,您需要呈现不同的视图,只需返回PartialView(same_view_name)实际上不会执行任何操作。
或者,在当前视图中检查请求是否是AJAX请求,然后有选择地呈现相关部分。
答案 1 :(得分:-1)
而不是重新调整View,你应该返回PartailView或字符串作为部分视图的返回类型是MvcHtmlString。
在控制器中
public string SomeAction(int? Page)
{
int PageSize = 5;
IQueryable<Dinner> dines = from dine in db.Dinners orderby dine.EventDate select dine;
var paginatedDinners = new PagingList<Dinner>(dines, Page ?? 0, PageSize);
return RenderPartialViewToString("PartialViewName",paginatedDinners )
}
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
你应该使用不同的动作来处理Ajax请求,因为它的返回类型不同,所以不能在“索引”中使用它。
通过将partail视图名称及其模型传递给 RenderPartialViewToString 方法,它将返回Partial的RenderHtml。
//actionLink
@Ajax.RouteLink(">>>","SomeAction",new { page = Model.PageIndex + 1 },new AjaxOptions { UpdateTargetId = "ULdinnerlist",InsertionMode =InsertionMode.Replace })