tags:

views:

30

answers:

2

Should be an easy question to answer. I am trying to create an object in a view. The class that contains the object consists of a User class and a password. When I click on the submit button, the Controller picks up null values for Password and User. See below the Container class, the Controller and the View;

public class UserExtended
{
    public UserITOC User { get; set; }
    public string Password { get; set; }
}

    [Authorize]
    public ActionResult Create()
    {
        return View(new UserExtended());
    }

    //
    // POST: /Dinners/Create

    [Authorize(Roles = "Administrator")]
    [HttpPost]
    public ActionResult Create(UserExtended user)
    {
        if (ModelState.IsValid)
        {
            // Create user in the User datatable
            SqlUsersRepository sqlRepository = new SqlUsersRepository();
            ITOCEntities db = new ITOCEntities();
            db.UserITOCs.AddObject(user.User);

            // Create user as an authenticated user within the Reader role.
            int i = user.User.EmailAddress.IndexOf('@') - 1;
            string userName = user.User.EmailAddress.Substring(0, i);
            string email = user.User.EmailAddress;
            Membership.CreateUser(userName, user.Password, email);
            Roles.AddUserToRole(userName, "Reader");                // Automatically assigned as a Reader
        }
        return View(new UserExtended());
    }

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

Create

<h2>Create</h2>

<% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.User.Forename) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.User.Forename)%>
            <%: Html.ValidationMessageFor(model => model.User.Forename)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.User.Surname) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.User.Surname)%>
            <%: Html.ValidationMessageFor(model => model.User.Surname)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.User.EmailAddress) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.User.EmailAddress)%>
            <%: Html.ValidationMessageFor(model => model.User.EmailAddress)%>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Password) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => model.Password)%>
            <%: Html.ValidationMessageFor(model => model.Password) %>
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>

A: 

you are returning a new instance of UserExtended class

return View(new UserExtended());

instead return the object you get as the parameter

return user

ajay_whiz
This doesn't really have anything to do with the fact that the instance he gets in to the action is null...
Yngve B. Nilsen
Yngvebn is correct
arame3333
@arame3333 did you try just returning the user var
ajay_whiz
@ajay_whiz - the issue here is NOT what is being returned from the Action, it's what is posted back to the [HttpPost] Create-action. The code never reaches the return-line, so there is no reason why it should help to return only the user var.
Yngve B. Nilsen
@Yngve okay!!! got it.
ajay_whiz
+2  A: 

Extremely simple solution:

Change your action-signature from

public ActionResult Create(UserExtended user)

to

public ActionResult Create(UserExtended UserExtended)

That way the ModelBinder will know how to reassemble the object from Request.

Hope this helps!

Yngve B. Nilsen
Fantastic! I thought the answer would be easy, but it is one of those mistakes you have to make when learning MVC, IMHO.
arame3333
So true :) Glad I could help!
Yngve B. Nilsen
@Yngve I had the same problem too for the last 3 days! Couldn't believe was so easy! Haha! Thanks!
BrunoSalvino
Glad I could help :)
Yngve B. Nilsen