Reputation: 740
Part of the website I'm doing (a simple turn-per-turn browser game) requires me to pass data from a View to a controller (passing an Foreign Key from a DropDownList and another integer value in the view as argument to the controller). Next the controller uses these values to pass execution to another controller.
My problem is that I have troubles figuring out how MVC returns the data inside my view and bundle inside types and use them as parameters. I know I could use an action link to point to my next controller but I do not know how to retrieve the selected value in the DropDownList. That or use an Input Button but then I do not know what or how the values are returned.
Do tell me if you require extra explanation or anything is confusing.
Here is my code :
The view in Razor, receiving a view model, defined below.
@model BattleForLurnia_MVC.Controllers.ChooseCharacterViewModel
@{
ViewBag.Title = "ChooseCharacter";
}
<h2>Choose Your Character</h2>
<fieldset>
<legend>Choose your fighter !</legend>
<div class="display-label">Your characters :</div>
<div class="display-field">
@Html.DropDownList("CharactersList", String.Empty)
</div>
@* Here is the part that returns the values, either an ActionLink or
an Input button.*@
</fieldset>
My ViewModel passed to the View
public class ChooseCharacterViewModel
{
Battle_For_LurniaEntities db = new Battle_For_LurniaEntities();
public Player player { get; set; }
public Character targetCharacter { get; set; }
public ChooseCharacterViewModel(string playerUsername, Character opponent)
{
player = db.Players.Single(plr => plr.Username == playerUsername);
targetCharacter = opponent;
}
public ChooseCharacterViewModel(){}
}
The controler that sends the View
public ActionResult ChooseCharacter(int targetCharacter_ID)
{
Character targetChar = db.Characters.Single(chr => chr.ID_Character == targetCharacter_ID);
ChooseCharacterViewModel chooseCharacter = new ChooseCharacterViewModel(Session["Username"].ToString(), targetChar);
// Using LINQ to get characters owned by the current player. Fuckyeah.jpg =D
IEnumerable<Character> blabla = from VAR in db.Characters
where VAR.FK_Player_ID == chooseCharacter.player.ID_Player
select VAR;
ViewBag.CharactersList = new SelectList(blabla, "ID_Character", "Name");
return View(chooseCharacter);
}
And finally the controller that receives the data in my view and sends another view.
[HttpPost]
public ActionResult ChooseCharacter(int FK_Character_ID, Character targetCharacter)
{
Character sourceCharacter = db.Characters.Single(chr => chr.ID_Character == FK_Character_ID);
return RedirectToAction("Fight", new {SourceCharacter = sourceCharacter, TargetCharacter = targetCharacter});
}
Upvotes: 0
Views: 689
Reputation: 2763
In your ViewModel, create three properties
When you POST back to your controller using a Html.BeginForm, the model will be passed back. For getting the Selected DropDown Value, change the syntax as below:
@Html.DropDownListFor(m=>m.SelectedId, new SelectList(Model.ListofItems))
For the OtherItemsThatYouNeedToGetBackAtController, You can use @Html.HiddenFor(m=>m.OtherItemsThatYouNeedToGetBackAtController)
Upvotes: 2