Reputation: 58
I am really new to MVC and I still have a WPF mindset. I have a controller that returns a model that has an ID as a parameter.
I would like to increment that parameter by pressing a button in the razor view and to reload another model that has that new Id.
I have tried something like:
public ActionResult Game() {
SequenceViewModel model = new SequenceViewModel(CardModelList, SelectedSequenceID);
return View(model);
}
and have another action on that button:
public ActionResult Press()
{
SelectedSequenceID ++;
return RedirectToAction("Game", "Game");
}
Even if my SelectedSequenceID seams to be set OK, the model still has the old value.
Is my approach completely wrong?
Thanks, Iulia
Upvotes: 2
Views: 3596
Reputation: 3588
You don't want to increment the ID in the controller, Just have a link to the next ID in the view!
MVC sticks a lot closer to HTTP ideals than webforms ever did, and HTTP is stateless, so you need to assume that state is NOT preserved between page request cycles (ok you can use session, cookies etc to do it, but only preserve what is absolutely necessary!)
ie
remember that the default routing will take the parameter "id" from the url /controller/action/id so an example controller might look like
public class GamesController : Controller
{
// eg. /Games/Game/1
public ActionResult Game(int id){
var SequenceModel = SequenceRepository.GetSequenceById(id); //some way of getting your model from the ID
return View(SequenceModel);
}
}
In the view you'd need to add links to navigate
@Html.ActionLink("Next", "Game", new {@id = Model.id + 1})
Or as a button:
<input type="button" value="Next" onclick="@Html.Raw("location.href='" + Url.Action("Next", new {@id = Model.id + 1}) + "';")" />
Upvotes: 1
Reputation: 21440
There are many ways, but one could be like this:
[HttpPost]
public ActionResult Press(string nextStep)
{
// not sure what this functionality does...
//SelectedSequenceID ++;
//return RedirectToAction("Game", "Game");
switch (step)
{
case "Intro":
// call method to run intro method or redirect to intro action
break;
default:
// return to start
break;
}
}
Then, put this in your razor view with the corresponding name:
<input type="submit" name="submitButton" value="Intro" />
Upvotes: 0