MVC3 Passing Model to Controller - Receiving Null values

10,295

Assuming you're trying to set values on 'newPlayer', then you can replace this

public player newPlayer = new player();

with this

public player newPlayer { get; set; }

The default model binder will create everything from the form on post-back--there's no need to instantiate it in the model.

Share:
10,295
jhartzell
Author by

jhartzell

Inspiring junior software develop with a focus on ASP.NET MVC3

Updated on June 04, 2022

Comments

  • jhartzell
    jhartzell almost 2 years

    I just started working with MVC3 a few weeks ago and being young in the programming ladder I'm still learning quite a bit. I've recently been working with Models, using TextBoxFor and other helpers to populate properties for my "Character" Model.

    Essentially what I'm trying to do is define a model and then pass it to my controller, however any property that I have defined as a static value in my Model is being passed as a null value on runtime.

    Below are some snippets of the parts needed to understand whats going on..

    Character.cs - Model
    
        // Instances of manipulated objects.
        otReal db = new otReal();
    
        public player newPlayer = new player();
    
        public byte[] conditions
        {
            get
            {
                return newPlayer.conditions;
            }
            set
            {
                byte[] array1 = null;
    
                array1 = new byte[16 * 12 * 3];            
    
                newPlayer.conditions = array1;
            }
        }
    
    CharacterController.cs
    
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Submit(Character c)
        {
            // Add a new character to the database via LINQ to Entities.         
            otReal.AddToplayers(c.newPlayer);
            otReal.players.AddObject(c.newPlayer);
            otReal.SaveChanges();
    
            return View();
        }
    

    The only helpers I have in my View are the ones that the user actually needs to interact with. If I go into my controller and set the values there they will get set to the correct value and it will insert. Any help would be greatly appreciated!

    Index.cshtml - View
    @using (Ajax.BeginForm("Submit", new AjaxOptions { OnComplete = "done" }))
    {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>New Character Information</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Name, "Character Name")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.TownList)
        </div>
        <div class="editor-field">
            @* @Html.DropDownList("TownList", ViewData["TownList"] as SelectList)*@
            @Html.DropDownListFor(model => model.TownList, ViewData["TownList"] as SelectList)
            @Html.ValidationMessageFor(model => model.TownList)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Sex)
        </div>
        <div class="editor-field">
            @Html.DropDownListFor(model => model.Sex, ViewData["sex"] as SelectList)
            @Html.ValidationMessageFor(model => model.Sex)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Vocation)
        </div>
        <div class="editor-field">
            @Html.DropDownList("VocList", ViewData["VocList"] as SelectList)
            @Html.ValidationMessageFor(model => model.Vocation)
        </div>
        <p>
            <input id="button" type="submit" value="Create" />
        </p>
        <div style="font-family: Tahoma; font-size: large" id="completeDiv" style="display: none;">
        </div>
        <span></span>
    </fieldset>
    

    }

    Basically what I'm trying to do here is create my model that has a bunch of base values that every 'Character' will have in the database table. This way when a player creates his/her character he/she will be able to enter a Character Name, Sex, Class(Vocation), Starting Town and all the other values will be populated behind the scenes and passed to the controller for creation.

  • jhartzell
    jhartzell about 13 years
    I really like the concept behind this idea ataddeini, however I'm not entirely sure how to set it up with my current properties as I am setting values of an instantiated object as to where your above example is a bit vague to me. I would love to learn more about it because it seems like exactly what I need.
  • ataddeini
    ataddeini about 13 years
    Ah, I think I understand your problem now. I would recommend using a separate view model for the portions of the data that the user can edit, and then combine this with any shared data before you persist to the database. You end up with more classes, but in the long run it keeps things cleaner. Here's some more information on the approach. Best of luck.
  • jhartzell
    jhartzell about 13 years
    Yea, I will probably take this approach. As a temporary fix just to test more with MVC I've just setup the Character with a constructor that sets all those values upon object creation.