如何通过PartialView的AJAX调用显示视图?

时间:2018-05-25 11:51:48

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

我正在使用PartialView,我正在部分显示3条新闻。我正在从partialview向我的行动结果发送新闻ID。当用户点击时,他们可以阅读被点击的新闻。我的ajax调用返回所有HTML数据。我很好,通常但我想获取数据库数据并显示我的动作结果。

NewsPartialView.cshtml

<script>
    $(document).ready(function () {
        $('.fright').click(function () {
            var id = $(this).attr('id');
            $.ajax(
                {
                    type: 'GET',
                    url: '/Content/NewsDetail/' + id,            


                    success: (function (data) {
                        alert("Okey");
                       console.log(data);

                    }),
                    error:function()
                    {

                        alert("Error");
                    }

                });

        });

    });
</script>

    @model IEnumerable<Models.Managers.News>
    .....
    <div class="news fright" id="@item.ID">Read New >></div>

NewsController

 public ActionResult NewsDetail(int id)
        {

            dbContext = new DbContext();

            var news = dbContext.News.First(x => x.ID == id);
            return View(news);

        }

2 个答案:

答案 0 :(得分:0)

您可以使用window.location.href。你不需要AJAX调用。

答案 1 :(得分:-1)

 $(document).ready(function () {
        $('.fright').click(function () {
var id = $(this).attr('id');
$.ajax({
         type: "GET",
        url: '/Content/NewsDetail',
        data: id,
        dataType: 'json',
        contentType: false,
        processData: false,
        success: function (data) {
        
        }
        error:function(data)
                    {

                        alert("Error");
                    }

    });
    });

    });

相关问题