0

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?)

1 Answers1

1

@Html.HiddenFor renders exactly one <input type="hidden"/>.

So if you want to post a complex object, make sure you render hidden fields for all properties of this object that shall be posted back. I don't know how your Area object looks like, but something along these lines:

@model CurrentArea 

@* post back the properties of CurrentArea.area we are interested in *@
@Html.HiddenFor(m => m.area.Name)
@Html.HiddenFor(m => m.area.Id)
@Html.HiddenFor(m => m.area.Coordinates.X)

@* post back the properties of CurrentArea.player ... *@
@Html.HiddenFor(m => m.player.Name)

See also Extend hiddenfor templates in asp net mvc.

Community
  • 1
  • 1
Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36
  • That fixed the problem for me. Thinking about it like that also sounds very logical in hindsight. Thanks you for helping me out! – R. Filippini Jan 24 '17 at 18:13