使用Jquery ajax在Controller中调用ActionResult方法并返回数据 [英] Using Jquery ajax to call ActionResult method in Controller and return data

查看:591
本文介绍了使用Jquery ajax在Controller中调用ActionResult方法并返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解Jquery Ajax方法的工作方式.现在,我在调用控制器中的ActionResult方法时遇到一些问题,该方法将返回PartialView.

I'm trying to understand how the Jquery Ajax methods works. Right now, I have some problems calling the ActionResult method in the controller that will return a PartialView.

已经创建了一个按钮,我将使用该按钮从服务器中获取新数据(应该运行Ajax调用)

Have created an button which I will use to get new data in from the server (Ajax call should run)

代码:(Home控制器中的ActionResult)

public ActionResult All()
    {
        List<Student> model = db.Students.ToList();

        return PartialView("_Student", model);
    }

当我在主索引视图中按下按钮时,我会尝试调用该方法.

This method is the one I'm trying to call when I press the button in the main Index view.

代码:(索引视图)

<div class="row">
<div class="small-12 column sbw-travel">
    <h2>Travel</h2>

    <div class="row">
        <div class="small-12 columns">
            <button id="button1" class="sbw-travel-button">Search</button>
        </div>
    </div>

    <div class="small-12 sbw-travel-box" id="rooms">

    </div>
</div>
</div>

当用户按下按钮时,应运行Ajax调用,并且该列表将显示在id = rooms的部分中.

When the user hit the button, an Ajax call should run, and the list will appear in the section with id=rooms.

脚本:(Ajax代码)

 $(document).ready(function () {
    $('#button1').click(function () {
        $.ajax({
            type: 'GET',
            url: @Url.Action("All", "Home"),
            datatype: "html",
            success: function () { 
                $('#rooms').html(???);
            }
        });
        return false;
    });
});

你们中的任何人都可以看到我是否忘记了要像我描述的那样进行这次跑步吗?

Can any of you see if I have forgot something to make this run like I have described?

推荐答案

结果在成功事件上.试试:

The result is on the succes event. Try:

$(document).ready(function () {
$('#button1').click(function () {
    $.ajax({
        type: 'GET',
        url: @Url.Action("All", "Home"),
        datatype: "html",
        success: function (data) { 
            $('#rooms').html(data);
        }
    });
    return false;
}); });

这篇关于使用Jquery ajax在Controller中调用ActionResult方法并返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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