4

(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!

3
  • actions result in a GET request being made. You have tagged the action with POST. Also if you do not want to refresh the view then use AJAX call Commented May 28, 2017 at 8:28
  • You have to remove [HttpPost] from second method. Commented May 28, 2017 at 8:35
  • I tried removing the HttpPost and it still doesn't update - I'll try messing with AJAX Commented May 28, 2017 at 8:37

1 Answer 1

2

The ChildActionOnly attribute ensures that an action method can be called only as a child method from within a view. so you no need to use [ChildActionOnly]. you are using a for go to the link so it's a GET request.

So remove

[HttpPost]
[ChildActionOnly]

And Change your return statement like:

return RedirectToAction("Pokemon", new {id = id});

Hopefully it's help for you.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! But I've still got some issues It looks like it's something to do with calling the method - I changed the return in Pokemon(int id, int steps) to return a static value, and the view did not reflect the changes. I don't think I'm calling the view correctly.
Thank you! The RedirectToAction is definitely going to solve issues I would have gotten later, but there's still one thing - Having both overloaded methods as httpgets is throwing an error - I'll see what happens if i just rename the pokemon(int id, int steps) Work's Perfect!

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.