如何在加载页面时使用ajax加载页面的一部分?

时间:2017-01-18 04:00:07

标签: jquery .net ajax asp.net-mvc

我想在页面加载时使用ajax从数据库加载一些数据。但它不起作用或显示数据。

我在控制器端的代码。

public ActionResult Index(int? respondentId)
{
   return View();
}

[HttpPost]
public ActionResult GetIntroParagraph(int? id)
{
    int id = id?? 111;
    Paragraph p= ParagraphDb.GetParagraghByRespondentId(id);
    return Content(p.Introduction);
}

我在视图上的代码,

<script>
    $(function () {
        var data = undefined;
        $.load(@Url.Action("GetIntroParagraph","Landing"),data,function(result) {
            $('#paragraph').append(result);
        });
    });
</script>

<div id="paragraph"></div>

我在哪里弄错了?

1 个答案:

答案 0 :(得分:0)

由于没有人回答,这是我发现自己的解决方案。

1.我必须在前面引用jquery文件。
2.使用$ .post而不是$ .load

@Scripts.Render("~/bundles/jquery")

<script>
$(function () {

    var data = undefined;
    $.post('@Url.Action("GetIntroParagraph","Landing")', data, function (result) {
        $('#paragraph').append(result);
    });
});
</script>