MVC ASP.NET Overloaded Controller Methods not working properly

Viewed 153

(Sorry if all these questions are stupid)

I have two methods in my controller

[HttpGet]
[Authorize]
public ActionResult Pokemon(int id)
{
    var user = db.PDCUsers
                 .SingleOrDefault(x => x.Username == User.Identity.Name);
    var pkmn = db.PlayerPkmns.SingleOrDefault(x => x.Id == id);

    return View(new DetailedPokemonViewModel(pkmn, user.Id, user.StepsIncMult));
}

and

[HttpPost]
[ChildActionOnly]
[Authorize]
public ActionResult Pokemon(int id, int steps)
{
    var user = db.PDCUsers.SingleOrDefault(x => x.Username == User.Identity.Name);
    var pkmn = db.PlayerPkmns.SingleOrDefault(x => x.Id == id);
    if (pkmn.CurrentTrainerId == user.Id)
    {
        pkmn.Experience = pkmn.Experience + steps;
        db.SaveChanges();
    }
    return Pokemon(id);
}

I'm calling the Pokemon(int id, int steps) from inside the Pokemon(int id) view by

<a href="@Url.Action("Pokemon", "PokemonView", new { id = Model.Id, steps = 5000 })">walk</a>

However, when I click the link the the Pokemon(int id, int steps), it doesn't update the database value - and when I put a breakpoint in it doesn't register. I don't think I'm even hitting the method, but the url at the top has the steps parameter concatenated?

All I'm trying to do is update the experience value of a data row by the amount of steps passed as a parameter. I don't want a new view (for now) - I just want it to show the Pokemon(int id) view again.

Is there a best practice for calling a method then returning to the view it was called from? Also, is there any obvious reason why my database values aren't updating/breakpoint isn't being hit?

Thanks


Edit:

By using RedirectToAction instead of returning the Pokemon(int id) method, and renaming the Pokemon(int id, int steps) to avoid overloading httpget issues, it works!

Thanks!

1 Answers
Related