从Controller调用方法而不重新加载视图?

时间:2018-03-15 08:44:18

标签: html asp.net-mvc

如何在" @ Html.ActionLink"上调用方法 - 在不重新加载视图的情况下从视图中选择参数?我正在使用ASP.Net MVC。我想,我必须使用AJAX,但我不知道它是如何工作的。

这是我的代码:查看:

@foreach (var p in Model.PresentedPresentations)
{                 
     @Html.ActionLink(" ", "AddPositiveRatingToPresentation", Presentation", new { htmlAttributes = new { @class = "glyphicon glyphicon-thumbs-down", @id ="posRat"}}, new { presentationId = p.Id }) @p.PositiveRatings  
}

控制器:

public void AddPositiveRatingToPresentation(int presentationId)
    {
        if (presentationId == 0)
        {
            Console.Write("ungültige presID");
        }
        var presentationService = new PresentationService();
        presentationService.AddPositiveRating(presentationId);
    }

1 个答案:

答案 0 :(得分:0)

下面的代码应该可以让您了解AJAX是如何实现的。

<强> HTML:

@foreach (var p in Model.PresentedPresentations)
{                 
     <a href="javascript:void(0)" class="glyphicon glyphicon-thumbs-down",id ="posRat", data-presentation = p.Id />  
     @p.PositiveRatings  
}

<强> JQUERY:

function AddPositiveRatingToPresentation(_this)
{
   //Get presentation Id
   var _presentationId = $(_this).data('presentation');

   //Create AJAX request
   $.ajax({
             url: '@Url.Action("AddPositiveRatingToPresentation")',
             type: 'GET',
             cache: false,
             data: { presentationId: _presentationId  },
             success: function (data) {
                        //Code goes here
                    }
         });
}