Create View - Publish Null Objects

There should be a simple question to answer. I am trying to create an object in a view. The class containing the object consists of the User class and password. When I click the "Submit" button, the controller gets the values ​​"Empty" for "Password" and "User". See below the class Container, Controller and 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> 

+4
source share
3 answers

Extremely simple solution:

Change your action signature from

 public ActionResult Create(UserExtended user) 

to

 public ActionResult Create(UserExtended UserExtended) 

This way the ModelBinder will know how to assemble the object from the request.

Hope this helps!

+12
source

I had a very similar problem, but in my case, I found that I need to match the table name of the table to the table of tables , not the type name

  • Type Name: NonTradingDay
  • Database table name: dbo.NonTradingDays (has been pluralized )

Create Method:

 [HttpPost] public ActionResult Create(NonTradingDay NonTradingDays) { if (ModelState.IsValid) { db.NonTradingDay.Add(NonTradingDays); db.SaveChanges(); return RedirectToAction("Index"); } return View(NonTradingDays); } 

I tried "NonTradingDay", but still got zero; Then I looked at the name of the database table, tried "NonTradingDays", and a match was made (the argument is no longer zero).

I thought because I had a database context like:

 public DbSet<NonTradingDay> NonTradingDay { get; set; } 

Instead

 public DbSet<NonTradingDay> NonTradingDays { get; set; } 
+3
source

you return a new instance of the UserExtended class

return View (new UserExtended ());

return the object you get as a parameter instead

return user

0
source

All Articles