更新面板在ASP.NET MVC 3 [英] Update Panel in ASP.NET MVC 3

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

问题描述

我在寻找一种方式在ASP.NET MVC 3,做一个更新面板我发现此链接:如何使更新面板在ASP.NET MVC ,但没有奏效。

I'm looking for a way to do a "Update Panel" in ASP.NET MVC 3. I found this link: How to make update panel in ASP.NET MVC but didn't work.

所以,我这样做,我认为:

So, i did this in my view:

<div>
    <input type="text" id="userName" />
    <button type="button" onclick="searchUserByName()">Search</button>
</div>
<div id="usersPanel">
    @{Html.RenderPartial("_UserList", Model);}
</div>
<script type="text/javascript">

    function searchUserByName() {
        var userName = $("#userName").val();

        $.post('@Url.Action("SearchUserByName")',
            {username: userName},
            function (htmlPartialView) {
                $("#usersPanel").html(htmlPartialView);
            }
        );
    }

</script>

在我的控制器:

public ActionResult SearchUserByName(string userName)
{
    List<User> users = // code to search users by name

    return PartialView("_UserList", users);
}

但我不知道,如果是做一个好(或右)的方式,或者,如果有一种方法用asp.net的MVC这样做3.有更好的方式来做到这一点,或者与ASP .NET MVC 3?

But i don't know if is a good (or right) way to do that, or if there is a way to do this with asp.net mvc 3. There is a better way to do this, or with asp.net mvc 3?

推荐答案

只需使用Ajax请求得到你的行动方法的结果。它基本上做同样的事情的更新面板在asp.net中。

Just use ajax request to get the results from your action methods. It basically does the same thing as update panels in asp.net.

所以像下面这样。

$.ajax({
async: false,
cache: false,
type: 'POST',
    url: /controller/action,
    data: { id: idParam },
    beforeSend: function (XMLHttpRequest) {
        if (confirmMessage !== undefined) {
            return confirm(confirmMessage);
        }
        return true;
    },
    success: function (data) {
        // do stuff
    },
    error: function () {
        alert('An error occured');
    }
});

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

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