I'm fairly new to working with ASP.NET MVC and I'm currently trying to solve a problem I just can't get my head around:
I'm trying to load a currentArea from my Database and create a CurrentArea object from it like this:
//My view ("PlayGame")
@model SE2_S2P2_opdracht.Models.CurrentArea
<html>
<body>
<div style="position:absolute; margin-top:8%; background-color:dimgrey; border:solid thin; border-color:darkgrey; width:59.45%; height:20%; opacity: 0.9">
@if (Model.area.GoNorth(Model.area.NorthMap))
{
using (Html.BeginForm("NextArea", "Game", new { direction = "North" }, FormMethod.Post))
{
<div style="position:absolute;top:8%; left:7%;">
@Html.HiddenFor(m => m.area)
@Html.HiddenFor(m => m.player)
<input id="submit" type="submit" value="Go North" class="btn" />
</div>
}
}
</div>
</body>
</html>
So when I press the Go North button it should trigger this:
(In summary: it should load a new CurrentArea based on the view-passed currentArea)
//My Controller ("GameController")
public ActionResult NextArea(CurrentArea currentarea, string direction)
{
if (direction == "North")
{
return View("PlayGame", currentarea = new CurrentArea(AreaRepo.GetByNorthMap(currentarea.area), currentarea.player));
}
After pressing the button I get a null reference saying that my CurrentArea (passed to the controller) is null. It does, however, read my direction string.
My CurrentArea Class is just a wrapper
public class CurrentArea
{
public Area area { get; set; }
public Player player { get; set; }
public Enemy enemy { get; set; }
public CurrentArea()
{
}
public CurrentArea(Area area, Player player)
{
this.area = area;
this.player = player;
}
I made sure all required class properties had "{get; set;}" on them as well. (The ones I use in the required constructors, at least)
I suppose I'm missing something which more experienced programmers can instantly spot. (by using the @html.hiddenfor() I can pass certain class objects/properties without showing them, right?)