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?

3
  • Regarding the side question (making it a button), see here: stackoverflow.com/questions/596444/… Commented Nov 4, 2013 at 17:39
  • what scripts are you referencing on your page? Commented Nov 4, 2013 at 17:48
  • make sure you have the ajax unobtrusive reference included on your page Commented Nov 4, 2013 at 18:07

2 Answers 2

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

1 Comment

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.
0

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

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.