Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create view is posting null objects

Tags:

asp.net-mvc

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());
    }
" %>

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>

like image 778
arame3333 Avatar asked Dec 08 '22 02:12

arame3333


1 Answers

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!

like image 145
Yngve B-Nilsen Avatar answered Jan 08 '23 18:01

Yngve B-Nilsen