Please bear with my noobness, I'm super new to Asp.NET MVC, I don't really understand it yet.
Basically what I want to do is to update a table in my database from information entered into a form.
I have a registration form with several options: FirstName
, LastName
, Age
, Sex
, SecretQuestion
, SecretQuestionAnswer
.
I'd like to insert this data in my UserProfile table (I use SimpleMembershipProvider) to the corresponding columns. Here's my complete Register ActionResult in my main controller:
[HttpPost]
[ValidateAntiForgeryToken]
[RecaptchaControlMvc.CaptchaValidatorAttribute]
public ActionResult Regisztracio(RegisterModel model, bool captchaValid, string captchaErrorMessage)
{
if (ModelState.IsValid)
{
if (!captchaValid)
{
ModelState.AddModelError("recaptcha", captchaErrorMessage);
return View(model);
}
try
{
OneMillionDb db = new OneMillionDb();
User user = new User();
var token = WebSecurity.CreateUserAndAccount(model.UserName, model.Password, null, true);
user.LastName = model.LastName;
user.FirstName = model.FirstName;
user.Age = model.Age;
user.Sex = model.Sex;
user.SecretQuestion = model.SecretQuestion;
user.SecretQuestionAnswer = model.SecretQuestionAnswer;
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
ModelState.AddModelError("UserName", "The username already exists.");
ModelState.AddModelError("Email", "The specified E-mail address already exists.");
ModelState.AddModelError("ConfirmEmail", "");
return View(model);
}
}
return View(model);
}
OneMillionDb
is my dbContext model file:
public class OneMillionDb : DbContext
{
public OneMillionDb()
: base("DefaultConnection")
{
}
public DbSet<User> Users { get; set; }
}
User
is my model class for table definiton. It looks like this:
[Table("UserProfile")]
public class User
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[Column]
[Required]
public string UserName { get; set; }
[Column]
public string FirstName { get; set; }
[Column]
public string LastName { get; set; }
[Column]
[Required]
public int Age { get; set; }
[Column]
public string Sex { get; set; }
[Column]
[Required]
public string SecretQuestion { get; set; }
[Column]
[Required]
public string SecretQuestionAnswer { get; set; }
[Column]
public int MoneyIn { get; set; }
[Column]
public int MoneyOut { get; set; }
[Column]
public int TimesWon { get; set; }
}
I used to work in ASP.NET Web Pages, in that I'd use the UPDATE
SQL statement to do this then execute the query..
Edit3: Here's my Register view:
@model OneMillion.Models.RegisterModel
@using Recaptcha;
@{
ViewBag.Title = "Register";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Register</legend>
<ul>
<li>
@Html.LabelFor(m => m.UserName)
@Html.EditorFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Email)
@Html.EditorFor(m => m.Email)
</li>
<li>
@Html.LabelFor(m => m.ConfirmEmail)
@Html.EditorFor(m => m.ConfirmEmail)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
</li>
<li>
@Html.LabelFor(m => m.LastName)
@Html.EditorFor(m => m.LastName)
</li>
<li>
@Html.LabelFor(m => m.FirstName)
@Html.EditorFor(m => m.FirstName)
</li>
<li>
@Html.LabelFor(m => m.Age)
@Html.EditorFor(m => m.Age)
</li>
<li>
@Html.LabelFor(m => m.Sex)
@Html.EditorFor(m => m.Sex)
</li>
<li>
@Html.LabelFor(m => m.SecretQuestion)
@Html.EditorFor(m => m.SecretQuestion)
</li>
<li>
@Html.LabelFor(m => m.SecretQuestionAnswer)
@Html.EditorFor(m => m.SecretQuestionAnswer)
</li>
<li>
@Html.Raw(Html.GenerateCaptcha())
</li>
</ul>
<input type="submit" value="Register" />
</fieldset>
}
And here's my RegisterModel:
public class RegisterModel
{
[Required]
[Display(Name = "Felhasználónév")]
public string UserName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "E-mail cím")]
public string Email { get; set; }
[DataType(DataType.EmailAddress)]
[Display(Name = "E-mail cím megerősítése")]
[Compare("Email", ErrorMessage = "A két e-mail cím nem egyezik.")]
public string ConfirmEmail { get; set; }
[Required]
[StringLength(100, ErrorMessage = "A jelszónak legalább {2} karakter hosszúnak kell lennie.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Jelszó")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Jelszó megerősítése")]
[Compare("Password", ErrorMessage = "A jelszó és a jelszó megerősítése mezők nem egyeznek.")]
public string ConfirmPassword { get; set; }
[Display(Name= "Vezetéknév")]
public string LastName { get; set; }
[Display(Name= "Keresztnév")]
public string FirstName { get; set; }
[Display(Name= "Életkor")]
public int Age { get; set; }
[Display(Name="Nem")]
public string Sex { get; set; }
[Required]
[Display(Name="Titkos kérdés")]
public string SecretQuestion { get; set; }
[Required]
[Display(Name="Titkos kérdés válasza")]
public string SecretQuestionAnswer { get; set; }
}
Edit: My Problem:
The database doesn't get updated. I know this since I get an Exception stating that Column Age can't have a value of NULL
, but the corresponding input was filled out.
Edit2: I also changed the code to reflect SOfanatic's answer, but I still get the error.
Edit4: Here's the exception I get in the Catch side of trycatch:
Cannot insert the value NULL into column 'Age', table 'OneMillionDb.dbo.UserProfile'; column does not allow nulls. INSERT fails.