7

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 database as well, so what is the best way to call the controller method from the Javascript?

3
  • 4
    Lots of ways. You could build a form with the data and post it, you could follow a GET link in the proper format, you could invoke an AJAX call... your question is not specific enough Commented Jun 8, 2012 at 13:41
  • 3
    submitting a POST or DELETE with AJAX is the way to go. destructive actions like deletes shouldn't be done with a GET Commented Jun 8, 2012 at 13:44
  • Can you show your controller method and how/where you are populating the movie id you'd like to delete in your javascript? Commented Jun 8, 2012 at 13:44

5 Answers 5

6

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>
Sign up to request clarification or add additional context in comments.

Comments

6

Depending on your code it could be as simple as:

$.post("/controller/method/" + id);

Comments

4

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) {

        }
    });
});

Comments

0

Try This,

function (){
   var url = '@Url.Action("SearchReoccurence", "SearchReoccurence", new { errormessage = "__msg__" })';
}

Comments

0

A bit late for the party, but maybe it will help someone. For me, on ASP.NET MVC 5 this work wonderfully:

Client jQuery side

let data1 = 'some text';
let data2 = 'some other text';

$.post('ControllerName/MethodName', { field1: data1, field2: data2 }, 
    function(response) {
    // Do something here with the data return from the controller
    }
});

field1 and field2 needed to be the exact same name written in your method fields.

It's best to user JsonResult as the return type from your controller's method and parse it with Json() method.

Controller's method example

[HttpPost]
public JsonResult Method(string field1, string field2)
{
    // Do something here with the data sent from the jQuery client side
    ClassExample example = new();
    return Json(example);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.