1

Right now I am trying to create a link that, upon the user clicking, would change a Boolean without calling up it's own view. Whenever I click the link though, it sends me to a view that doesn't exist. Can anyone find what I'm doing wrong to make sure it stays in the current view and performs the action?

The cshtml:

@Ajax.ActionLink("Hide",
"Hide",
"Manager",
new { id = item.MenuID },
new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "function() { alert('The item has been hidden')"
})

The controller:

[HttpPost]
public ActionResult Hide(int id)
{
    Menu menu = db.Menus.Find(id);
    if (menu == null)
    {
        return HttpNotFound();
    }
    menu.Display = false;
    db.Entry(menu).State = EntityState.Modified;
    db.SaveChanges();
    return new EmptyResult();
}

Also, a slightly related side question, is there a way to make the link into a button?

tereško
  • 58,060
  • 25
  • 98
  • 150
Mechalon
  • 33
  • 4

2 Answers2

1

Most likely, your page is missing one of the following script. Make sure you reference them all:

<script src="/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
Dima
  • 6,721
  • 4
  • 24
  • 43
  • Huh, completely forgot to do the script includes. Fixed it right up. Now to find a way to make it refresh the table after the change has been made. – Mechalon Nov 05 '13 at 01:27
0

Use void instead of ActionResult for the Hide method in your controller (and remove the "return" clause, of course).

Tomaz Tekavec
  • 764
  • 6
  • 22