0
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(">>>",new { page = Model.PageIndex + 1 },new 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);
        }

问题是当我进行分页时它会呈现整个视图。谢谢

4

2 回答 2

0

要返回不同的信息以响应您的 AJAX 请求,您需要呈现不同的视图,仅返回 PartialView(same_view_name) 实际上不会做任何事情。

或者,在您当前的视图中检查请求是否是 AJAX 请求,然后有选择地呈现相关部分。

于 2012-10-22T05:23:12.420 回答
-1

而不是重新调整视图,您应该返回 ParttailView 或字符串,因为部分视图的返回类型是 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 请求,因为它的返回类型不同,所以不能在“索引”中使用它。

通过将部分视图名称及其模型传递给RenderPartialViewToString方法,它将返回您的部分 RenderHtml。

//actionLink
    @Ajax.RouteLink(">>>","SomeAction",new { page = Model.PageIndex + 1 },new AjaxOptions { UpdateTargetId = "ULdinnerlist",InsertionMode =InsertionMode.Replace })
于 2012-10-22T05:31:58.383 回答