如何在 ASP.NET MVC 中制作更新面板 [英] How to make update panel in ASP.NET MVC

查看:19
本文介绍了如何在 ASP.NET MVC 中制作更新面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 ASP.NET Model-View-Contoller (MVC) 中制作更新面板 框架?

解决方案

您可以在 ASP.NET MVC 中使用分部视图来获得类似的行为.分部视图仍然可以在服务器上构建 HTML,您只需将 HTML 插入适当的位置(事实上,如果您愿意包含 MSFT Ajax 库,MVC Ajax 助手可以为您设置).

在主视图中,您可以使用 Ajax.Begin 表单来设置异步请求.

 <% using (Ajax.BeginForm("Index", "Movie",新的 Ajax 选项 {OnFailure="searchFailed",HttpMethod="GET",UpdateTargetId="电影表",})){%><input id="searchBox" type="text" name="query"/><input type="submit" value="搜索"/><%}%><div id="电影表"><% Html.RenderPartial("_MovieTable", Model);%>

局部视图封装了您要更新的页面部分.

<%@ Control Language="C#" Inherits="ViewUserControl>"%><表格><tr><th>标题</th><th>发布日期</th></tr><% foreach(模型中的变量项){%><tr><td><%=Html.Encode(item.Title)%></td><td><%= Html.Encode(item.ReleaseDate.Year)%></td></tr><%}%>

然后设置您的控制器操作来处理这两种情况.局部视图结果适用于异步请求.

public ActionResult Index(string query){var 电影 = ...如果 (Request.IsAjaxRequest()){返回 PartialView("_MovieTable", 电影);}返回视图(索引",电影);}

希望有所帮助.

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

解决方案

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",    
                            }))

       { %>
            <input id="searchBox" type="text" name="query" />
            <input type="submit" value="Search" />            
    <% } %>

    <div id="movieTable">
        <% Html.RenderPartial("_MovieTable", Model); %>
   </div>

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

<%@ Control Language="C#" Inherits="ViewUserControl<IEnumerable<Movie>>" %>

<table>
    <tr>       
        <th>
            Title
        </th>
        <th>
            ReleaseDate
        </th>       
    </tr>
    <% foreach (var item in Model)
       { %>
    <tr>        
        <td>
            <%= Html.Encode(item.Title) %>
        </td>
        <td>
            <%= Html.Encode(item.ReleaseDate.Year) %>
        </td>       
    </tr>
    <% } %>
</table>

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.

这篇关于如何在 ASP.NET MVC 中制作更新面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆