Search code examples
ajaxasp.net-mvc-3asp.net-mvc-ajax

MVC 3 Ajax.ActionLink Pass Parent Model Property


I have a view which displays a model eg:

public class MyModel()
{
  public string name {get;set;}
  public IList<Note> notes {get;set;}
}

The view displays all the notes for the model, i am trying to use Ajax.ActionLink to delete a note, but in order to delete a note i need to pass my controller action result the ID of the model.

public ActionResult DeleteNote(int modelId, int noteId)
{
  var franchise = _franchiseRepository.FindById(modelId);

  Note note = new Note(noteId);

  franchise.RemoveNote(note);
  _franchiseRepository.SaveOrUpdate(franchise);

  return View();
}

Ajax.ActionLink("Delete", "DeleteNote", new {id=item.id}, new AjaxOptions{HttpMethod="POST"})

Can this be accomplished with ajax.actionlink?

Thanks in advance


Solution

  • Your DeleteNote action expects two parameters. I think it should work if you change the ActionLink to:

    Ajax.ActionLink("Delete", "DeleteNote", new { modelId = [modelId], noteId = [noteId] }, new AjaxOptions { HttpMethod = "POST" })