How to call a controller method from Javascript

前端 未结 4 1368
甜味超标
甜味超标 2021-01-18 10:51

I am displaying a bunch of movies in a table, I am eventually deleting each movie through Javascript which hides the div.

I now want to delete the movie from the d

相关标签:
4条回答
  • 2021-01-18 11:15

    Have an HTTPPost action method to delete in your movie controller

    [HttpPost]
    public ActionResult Delete(int id)
    {
      try
      {
        repo.DeleteMovie(id);
        return "deleted"
      }
      catch(Exception ex)
      {
        //Log errror
      }
      return "failed";
    }
    

    And in your View,

    <a href="#" data-movieId="34" class="movie">Delete Avengers</a>
    <a href="#" data-movieId="35" class="movie">Delete Iron Man</a>
    <script type="text/javascript">
    $(function(){
    
       $(".movie").click(function(e){
         e.preventDefault();
         $.post("@Url.Action("Delete","Movie")", { id : $(this).data("movieId")} ,function(data){
            alert(data);
         });
       });
    });
    
    </script>
    
    0 讨论(0)
  • 2021-01-18 11:17

    Try this: (Using jQuery Ajax)

    $("#DeleteButtonID").on("click", function() {
        $.ajax(
        {
            type: "POST",
            page: 1,
            rp: 6,
            url: '@Url.Action("PopulateDataListWithHeader", "DataList")' + "?id=" + YOURID,
            dataType: "json",
            success: function(result) {
    
            },
            error: function(x, e) {
    
            }
        });
    });
    
    0 讨论(0)
  • 2021-01-18 11:28

    Try This,

    function (){
       var url = '@Url.Action("SearchReoccurence", "SearchReoccurence", new { errormessage = "__msg__" })';
    }
    
    0 讨论(0)
  • 2021-01-18 11:32

    Depending on your code it could be as simple as:

    $.post("/controller/method" + id);
    
    0 讨论(0)
提交回复
热议问题