How to make update panel in ASP.NET MVC

前端 未结 3 1716
天涯浪人
天涯浪人 2020-12-04 06:24

How do I make an update panel in the ASP.NET Model-View-Contoller (MVC) framework?

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 06:30

    You could use a partial view in ASP.NET MVC to get similar behavior. The partial view can still build the HTML on the server, and you just need to plug the HTML into the proper location (in fact, the MVC Ajax helpers can set this up for you if you are willing to include the MSFT Ajax libraries).

    In the main view you could use the Ajax.Begin form to setup the asynch request.

        <% using (Ajax.BeginForm("Index", "Movie", 
                                new AjaxOptions {
                                   OnFailure="searchFailed", 
                                   HttpMethod="GET",
                                   UpdateTargetId="movieTable",    
                                }))
    
           { %>
                
                            
        <% } %>
    
        
    <% Html.RenderPartial("_MovieTable", Model); %>

    A partial view encapsulates the section of the page you want to update.

    <%@ Control Language="C#" Inherits="ViewUserControl>" %>
    
    
        <% foreach (var item in Model)
           { %>
        
        <% } %>
    
    Title ReleaseDate
    <%= Html.Encode(item.Title) %> <%= Html.Encode(item.ReleaseDate.Year) %>

    Then setup your controller action to handle both cases. A partial view result works well with the asych request.

    public ActionResult Index(string query)
    {          
        var movies = ...
    
        if (Request.IsAjaxRequest())
        {
            return PartialView("_MovieTable", movies);
        }
    
        return View("Index", movies);      
    }
    

    Hope that helps.

提交回复
热议问题