I have a bit of a problem, When a user registers on my site they put in email/password then go onto another for for there details, addres etc, however after inputting details and pressing save it flips straight back to the initial login saying "enter email" "enter password" so it doesn't save the customers details only email/password. Here is my controller-
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register()
{
return View();
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// Comment the following line to prevent log in until the user is confirmed.
// await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account");
ViewBag.Message = "Check your email and confirm your account, you must be confirmed "
+ "before you can log in.";
return View("Create");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
// GET: Create
public ActionResult Create()
{
return View();
}
// POST: Customers/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CustomerId,AccountId,FirstName,LastName,Address,City,PostalCode,Country,Phone,Mobile")] Customer customer)
{
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
customer.AccountId = user.Id;
db.Customers.Add(customer);
db.SaveChanges();
return View("Index");
}
Here is my customer Model -
public class Customer
{
[ScaffoldColumn(false)]
public string AccountId { get; set; }
[Key]
[ScaffoldColumn(false)]
public int CustomerId { get; set; }
[Required]
[StringLength(16, ErrorMessage = "Your name is too long")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Your last name is required.")]
[StringLength(16, ErrorMessage = "Last name is too long.")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required(ErrorMessage = "Address is required.")]
public string Address { get; set; }
[Required(ErrorMessage = "City is required.")]
public string City { get; set; }
[Required(ErrorMessage = "Postcode is required.")]
[Display(Name = "Post Code")]
public string PostalCode { get; set; }
[Required(ErrorMessage = "Country is required.")]
public string Country { get; set; }
[Required(ErrorMessage = "Phone home number is required.")]
[Display(Name = "Home Telephone")]
public string Phone { get; set; }
[Required(ErrorMessage = "Phone mobile number is required.")]
[Display(Name = "Mobile")]
public string Mobile { get; set; }
}
And my Razor view -
#model T_shirt_Company_v3.Models.RegisterViewModel
#{
ViewBag.Title = "Register";
}
<h2>#ViewBag.Title.</h2>
#using (Html.BeginForm("Register", "Account", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
<h4>Create a new account.</h4>
<hr />
#Html.ValidationSummary("", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(m => m.Email, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.TextBoxFor(m => m.Email, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.Password, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.PasswordFor(m => m.Password, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(m => m.ConfirmPassword, new { #class = "col-md-2 control-label" })
<div class="col-md-10">
#Html.PasswordFor(m => m.ConfirmPassword, new { #class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Register" />
</div>
</div>
}
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
"Create" part to add details - view
#model T_shirt_Company_v3.Models.Customer
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Customer</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.FirstName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FirstName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.LastName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.LastName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.LastName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Address, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Address, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Address, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.City, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.City, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.City, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.PostalCode, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.PostalCode, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.PostalCode, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Country, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Country, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Country, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Phone, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Phone, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Phone, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Mobile, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Mobile, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
Related
I have a quick question I am updating my user and there role and my modelstate.isvalid is failing. So the user does not get updated in the database. I don’t have anything [required] that isn’t being entered in a textbox even when I try to enter all fields it still fails. Not sure why.
Here is my data model, my controller action for editing / updating a user and there role and also here is my view with all the controls for inside the view.
Im realy not sure why my modelstate isnt valid i'm not doing anything complicated its just a simple U in CRUD and the action controller isnt validiting the model.
public class UpdateUserViewModel
{
public string UserId { get; set; }
[Display(Name = "User ID")]
public string IdShortened { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[Display(Name = "Username")]
public string UserName { get; set; }
[Display(Name = "Your Name")]
public string Name { get; set; }
[Display(Name = "Phone Number")]
public string PhoneNumber { get; set; }
[DataType(DataType.DateTime),
DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}",
ApplyFormatInEditMode = true)]
[Display(Name = "Birthday")]
public DateTime Birthday { get; set; }
[Display(Name = "Date Created")]
public DateTime? DateCreated { get; set; }
[Required]
[Display(Name = "User Roles")]
public string UserRoles { get; set; }
}
public async Task<ActionResult> EditSuperAdmin(string id)
{
if(id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
var user = await manager.FindByIdAsync(id);
if(user == null)
{
return HttpNotFound();
}
var userRoles = await manager.GetRolesAsync(user.Id);
ViewBag.Roles = new SelectList(
context.Roles.ToList(), "Name", "Name");
//new SelectList(context.Roles.Where(u =>
//!u.Name.Contains("SuperAdmin")).ToList(), "Name", "Name");
return View(new UpdateUserViewModel()
{
UserId = user.Id,
IdShortened = user.Id.Substring(0, 10),
Email = user.Email,
UserName = user.UserName,
Name = user.Name,
PhoneNumber = user.PhoneNumber,
Birthday = user.Birthday,
DateCreated = user.DateCreated,
UserRoles = manager.GetRoles(user.Id).FirstOrDefault()
});
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditSuperAdmin([Bind]UpdateUserViewModel model)
{
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
if (ModelState.IsValid)
{
var user = manager.FindById(model.UserId);
if (user == null)
{
return HttpNotFound();
}
user.Email = model.Email;
user.UserName = model.UserName;
user.Name = model.Name;
user.PhoneNumber = model.PhoneNumber;
user.Birthday = model.Birthday;
user.DateCreated = Convert.ToDateTime(model.DateCreated);
var roleResult =
manager.AddToRole(user.Id, model.UserRoles);
if (!roleResult.Succeeded)
{
TempData["ErrorRole"] = "Error adding User Role";
return RedirectToAction("Dashboard");
}
manager.Update(user);
context.SaveChanges();
TempData["Success"] = "User Updated Successfully";
return RedirectToAction("GetAllUsers", "SuperAdmin");
}
TempData["Error"] = "User Update Failed";
return RedirectToAction("Dashboard");
} #model MVC_TimeSh.Models.UpdateUserViewModel
#{
ViewData["Title"] = "Update User";
}
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="text-center">
#if (User.IsInRole("SuperAdmin"))
{
<h2>Update Super Administrator</h2>
}
else
{
<h3>Update | Edit User Account</h3>
}
</div>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(m => m.UserId)
<div class="form-group">
#Html.LabelFor(model => model.IdShortened,
htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.IdShortened, new { htmlAttributes =
new { #class = "form-control", #readonly = "readonly" } })
#Html.ValidationMessageFor(model => model.IdShortened, "",
new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Name,
htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Name,
new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Name, "",
new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Email,
htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Email, new { htmlAttributes =
new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Email, "",
new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserName,
htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.UserName,
new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.UserName, "",
new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.PhoneNumber,
htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.PhoneNumber,
new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.PhoneNumber, "",
new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Birthday,
htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Birthday,
new { htmlAttributes = new { #class = "form-control datepicker" } })
#Html.ValidationMessageFor(model => model.Birthday, "",
new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.DateCreated,
htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.DateCreated, new { htmlAttributes =
new { #class = "form-control", #readonly = "readonly" } })
#Html.ValidationMessageFor(model => model.DateCreated, "",
new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserRoles,
htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("UserRoles", (SelectList)ViewBag.Roles, "-- SELECT --")
#*new { htmlAttributes = new { #class = "form-control" } })*#
#Html.ValidationMessageFor(model => model.UserRoles, "",
new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-success" />
</div>
</div>
</div>
Sorry don't have 50 reputations can't comment.
As Chetan said debug and check the ModelState errors and check if UserRoles property is the issue.
it will be fine if you go with entity framework. Everything works fine there.
I'm working with documents. I've got working CRUD for DocumentViewModel - good so far. There will be other models that have documents as properties. I don't want to repeat myself, so I converted my Edit View of DocumentViewModel into an editor template.
Now I'm working on LeaseViewModel, which has a property LeaseDocument of type DocumentViewModel. The scaffolded Create view pulls the editor template in correctly. However, I get a problem in the POST Create method in the controller. All the simple LeaseViewModel properties are populated, and most of the sub-properties on the LeaseDocument property are populated, but DocumentUpload is null. Any thoughts?
View Models:
public class DocumentViewModel
{
#region Properties
public int? ItemID { get; set; }
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
public string Description { get; set; }
[Display(Name = "Version Number")]
public int Version_Number { get; set; }
// TODO: culture code?
[Display(Name = "Document")]
[DataType(System.ComponentModel.DataAnnotations.DataType.Upload)]
public HttpPostedFileBase DocumentUpload { get; set; }
...
}
public class LeaseViewModel
{
#region Properties
public int ItemID { get; set; }
[Display(Name = "Space")]
[Required]
public int SpaceID { get; set; }
[Display(Name = "Status")]
[Required]
public int StatusID { get; set; }
public string StatusText { get; private set; }
[Display(Name = "Type")]
[Required]
public int TypeID { get; set; }
public string TypeText { get; private set; }
public DocumentViewModel LeaseDocument { get; set; }
...
}
Controller:
[Authorize]
public class LeasesController : Controller
{
...
// POST: Lease/Create
[HttpPost]
[ValidateAntiForgeryToken]
[ValidateInput(true)]
public ActionResult Create(LeaseViewModel vm)
{
LeaseItem created = createLeaseVersion(vm);
if (created == null)
return View(vm);
else
return RedirectToAction("Index");
}
...
}
View:
#using CMS.CustomTables;
#using CMS.CustomTables.Types.Tenantportal;
#using CMS.DocumentEngine.Types.Tenantportal;
#model TenantPortal.Models.LeaseViewModel
#{
ViewBag.Title = "Documents";
var emptySpaceOpts = new SelectList(new List<CustomTableItem>(), "ItemID", "Identifier");
var tenantOpts = new SelectList(TenantProvider.GetTenants(), "ItemID", "Display_Name");
var statusOpts = new SelectList(CustomTableItemProvider.GetItems<LeaseStatusItem>(), "ItemID", "Name");
var typeOpts = new SelectList(CustomTableItemProvider.GetItems<LeaseTypeItem>(), "ItemID", "Name");
}
<h2 class="page-title">Create Lease</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.EditorFor(model => model.LeaseDocument)
<div class="form-group">
#Html.LabelFor(model => model.SpaceID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.SpaceID, emptySpaceOpts, "(select Property)")
#Html.ValidationMessageFor(model => model.SpaceID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.StatusID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.StatusID, statusOpts)
#Html.ValidationMessageFor(model => model.StatusID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.TypeID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.TypeID, typeOpts)
#Html.ValidationMessageFor(model => model.TypeID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ExecutionDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ExecutionDate)
#Html.ValidationMessageFor(model => model.ExecutionDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CommenceDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.CommenceDate)
#Html.ValidationMessageFor(model => model.CommenceDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ExpirationDate, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ExpirationDate)
#Html.ValidationMessageFor(model => model.ExpirationDate, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.CanViewCapBudget, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<label>
#Html.RadioButtonFor(model => model.CanViewCapBudget, "true")
Yes
</label>
<label>
#Html.RadioButtonFor(model => model.CanViewCapBudget, "false", htmlAttributes: new { #checked = "checked" })
No
</label>
#Html.ValidationMessageFor(model => model.CanViewCapBudget, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
Editor Template for DocumentViewModel
#using TenantPortal.Models
#using CMS.DocumentEngine.Types.Tenantportal
#model DocumentViewModel
#{
var signedOpts = DocumentViewModel.GetSignedOpts();
var propertyOpts = new SelectList(PropertyProvider.GetProperties().Columns("ItemID", "Identifier"), "ItemID", "Identifier");
var tenantOpts = new SelectList(TenantProvider.GetTenants(), "ItemID", "Display_Name");
bool isEdit = ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString() == "Edit";
object uploaderAtts;
int version;
if (isEdit)
{
uploaderAtts = new { #type = "file", #class = "form-control" };
Model.Version_Number += 1;
version = Model.Version_Number;
}
else
{
uploaderAtts = new { #type = "file", #required = "required", #class = "form-control" };
version = 1;
}
}
#Html.HiddenFor(model => model.ItemID)
<div class="form-group row">
<div class="col-sm-12 col-md-6 field">
#Html.LabelFor(model => model.Name, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.Name, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Name, "", new { #class = "text-danger" })
</div>
<div class="col-sm-12 col-md-6 field">
#Html.LabelFor(model => model.Description, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.Description, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Description, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group row">
<div class="col-sm-12 col-md-6 field">
#Html.LabelFor(model => model.Version_Number, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.Version_Number, new { htmlAttributes = new { #class = "form-control", #readonly = "readonly", #Value = version } })
#Html.ValidationMessageFor(model => model.Version_Number, "", new { #class = "text-danger" })
</div>
<div class="col-sm-12 col-md-6 field">
#Html.LabelFor(model => model.DocumentUpload, htmlAttributes: new { #class = "control-label" })
#(isEdit ? Html.Raw(String.Format("<a href='{0}' download='{1}'>{1}</a>", Model.Document_Url, Model.Original_File_Name)) : new HtmlString(""))
#Html.TextBoxFor(model => model.DocumentUpload, uploaderAtts)
#Html.ValidationMessageFor(model => model.DocumentUpload, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group row">
<div class="col-sm-12 col-md-4 field">
#Html.LabelFor(model => model.Signed, htmlAttributes: new { #class = "control-label" })
#Html.DropDownListFor(model => model.Signed, signedOpts, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Signed, "", new { #class = "text-danger" })
</div>
<div class="col-sm-12 col-md-4 field">
#Html.LabelFor(model => model.PropertyID, htmlAttributes: new { #class = "control-label" })
#Html.DropDownListFor(model => model.PropertyID, propertyOpts, "(none)")
#Html.ValidationMessageFor(model => model.PropertyID, "", new { #class = "text-danger" })
</div>
<div class="col-sm-12 col-md-4 field">
#Html.LabelFor(model => model.TenantID, htmlAttributes: new { #class = "control-label" })
#Html.DropDownListFor(model => model.TenantID, tenantOpts, "(none)")
#Html.ValidationMessageFor(model => model.TenantID, "", new { #class = "text-danger" })
</div>
</div>
If your form is sending a file, you have to set its enctype to multipart/form-data
e.g.
#using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
...
Hth...
I'm trying to learn ASP.net MVC. I'm using the pre-populated asp.net project that has a fake website within it to start. I have a form, and on submit, I want to put the values into a table in my database. I would also like to add functionality that if the email already exists in the database, I redirect them to another page.
I've added my own view, model, and controller.
Here's my databases (Side question: should I just put the table I created into DefaultConnection instead of Users.mdf?)
My Model:
public class RegisterLoyaltyViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public string Password { get; set; }
public string Phone { get; set; }
public string SecurityQuestion { get; set; }
public string SecurityAnswer { get; set; }
public string optSweepstakes { get; set; }
public string optEmails { get; set; }
}
My relevant controller code:
// POST: /Account/RegisterLoyalty
[HttpPost]
[AllowAnonymous]
public void RegisterLoyalty(RegisterLoyaltyViewModel model)
{
//Currently nothing here
}
I think what I need to do is hit the database inside the controller. I need to check if email already exists in the table, and if it does, redirect to page x. If email does not exist, simply submit the model to the database, and redirect to page y.
You need a data layer implementation to hit the database, there are various ways of doing it one of which is using Entity Framework, have a look at here to get some ideas, or alternatively you can use ADO.NET to connect to the database.
Create a controller action to GET the RegisterLoyalty view. That view contains a the RegisterLoyalty form. Have the form POST to the RegisterLoyalty action. Then you can perform your logic and add the model to the db if necessary.
//
// GET: /Account/RegisterLoyalty
[AllowAnonymous]
public ActionResult RegisterLoyalty()
{
return View();
}
//
// POST: /Account/RegisterLoyalty
[HttpPost]
[AllowAnonymous]
public ActionResult RegisterLoyalty(RegisterLoyaltyViewModel model)
{
var db = new AccountLoyaltyDbContext();
var emailExists = db.Loyalties.Any(x => x.EmailAddress == model.EmailAddress);
if (emailExists)
{
return RedirectToAction("X");
}
db.Loyalties.Add(model);
db.SaveChanges();
return RedirectToAction("Y");
}
//
// GET: /Account/X
[AllowAnonymous]
public ActionResult X()
{
return View();
}
//
// GET: /Account/Y
[AllowAnonymous]
public ActionResult Y()
{
return View();
}
RegisterLoyalty.cshtml
#model UserLoyalty.Models.RegisterLoyaltyViewModel
#{
ViewBag.Title = "RegisterLoyalty";
}
<h2>RegisterLoyalty</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>RegisterLoyaltyViewModel</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.FirstName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.FirstName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.LastName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.LastName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.LastName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.EmailAddress, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.EmailAddress, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Password, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Password, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Password, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Phone, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Phone, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Phone, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.SecurityQuestion, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SecurityQuestion, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SecurityQuestion, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.SecurityAnswer, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.SecurityAnswer, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SecurityAnswer, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.optSweepstakes, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.optSweepstakes, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.optSweepstakes, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.optEmails, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.optEmails, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.optEmails, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
So here is some code:
Controller
[HttpGet]
public ActionResult GetSettings()
{
var save = new SettingsSaver();
var dto = save.GetSettings();
var model = new SettingsModel
{
Password = dto.Password,
Port = dto.Port,
Username = dto.Username,
Enabled = dto.Enabled,
Id = dto.Id,
IpAddress = dto.IpAddress,
};
return View(model);
}
[HttpPost]
public ActionResult GetSettings(SettingsModel viewModel)
{
if (ModelState.IsValid)
{
var dto = new SettingsDto
{
IpAddress = viewModel.IpAddress,
Password = viewModel.Password,
Port = viewModel.Port,
Username = viewModel.Username,
Enabled = viewModel.Enabled,
Id = viewModel.Id
};
var save = new SettingsSaver();
var result = save.SaveSettings(dto); //Saves correctly and updates in DB
if (result)
{
return View(); // Returns this
}
return View("Error");
}
return View("Error");
}
View (Default Edit View)
#model Dash.UI.Models.Settings.SettingsModel
#{
ViewBag.Title = "Settings";
}
<h2>Settings</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Settings</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.Id)
<div class="form-group">
#Html.LabelFor(model => model.Enabled, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
#Html.EditorFor(model => model.Enabled)
#Html.ValidationMessageFor(model => model.Enabled, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.IpAddress, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.IpAddress, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.IpAddress, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Port, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Port, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Port, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Username, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Username, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Username, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Password, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Password, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Password, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
So, what the problem is when I update the model and POST to GetSettings it all works correctly, updates in the db etc. but on the return View() it does not hit the GetSettings() action method, but it returns the view with all of the model filled in except the password.
Model
public class SettingsModel : BaseSettingsViewModel // Base contains ID and Enabled properties with no data annotations
{
[Required]
[DataType(DataType.Text)]
[DisplayName("IP Address")]
public string IpAddress { get; set; }
[Required]
[DisplayName("Port")]
public int Port { get; set; }
[Required]
[DataType(DataType.Text)]
[DisplayName("Username")]
public string Username { get; set; }
[Required]
[DataType(DataType.Password)]
[DisplayName("Password")]
public string Password { get; set; }
}
Any advise/guidance would be much appreciated.
Upon returning the same view from a POST, the ModelState will fill in the controls (apart from the password fields) from the posted values.
So you need a Post-Redirect-Get pattern:
if (result)
{
return RedirectToAction("GetSettings");
}
Hi I've got the following code in my controller (asp.net 4.5.1 mvc 5) that allows a user to register on my site. Everything was working fine but Ive added another controller and another service and now when ever I try to register all it does on submit is redirect back to the form blank again. After debugging, the post action method in controller below is never called
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
UserManager.AddClaim(user.Id, new Claim(ClaimTypes.GivenName, model.FirstName));
var service = new GRCMemberService(HttpContext.GetOwinContext().Get<ApplicationDbContext>());
service.CreateGRCMember(model.FirstName, model.LastName, model.Address1, model.Address2, model.City, model.County, model.Postcode, model.Telephone, model.DateOfBirth, model.Dietary, model.CompLicenceNo, model.SelectedLicenceTypeId, model.NOKFirstName, model.NOKLastName, model.NOKTelephone, model.RelationshipTypeId, model.OtherOrgsGRC, model.OtherClubEvents, model.OtherOrgsOutside, user.Id);
//var currentUser = UserManager.FindByName(user.Id);
//var newrole = ("GRCMember");
//var roleresult = UserManager.AddToRole(currentUser.Id, newrole);
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking here");
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
instead, the get action method below is always called every time I submit the form:
[AllowAnonymous]
public ActionResult Register()
{
return View();
}
Any idea what's wrong?
Below is my ViewModel and View code.
ViewModel
public class RegisterViewModel
{
[Required]
[Display(Name = "First Name")]
[StringLength(160, ErrorMessage = "First Name cannot be longer than 160 characters.")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
[StringLength(160, ErrorMessage = "Last Name cannot be longer than 160 characters.")]
public string LastName { get; set; }
[Required]
[Display(Name = "Address 1")]
[StringLength(160, ErrorMessage = "Address1 cannot be longer than 160 characters.")]
public string Address1 { get; set; }
[Display(Name = "Address 2")]
[StringLength(160, ErrorMessage = "Address2 cannot be longer than 160 characters.")]
public string Address2 { get; set; }
[Required]
[Display(Name = "City")]
[StringLength(100, ErrorMessage = "City cannot be longer than 100 characters.")]
public string City { get; set; }
[Display(Name = "County")]
[StringLength(100, ErrorMessage = "County cannot be longer than 100 characters.")]
public string County { get; set; }
[Required]
[Display(Name = "PostCode")]
[StringLength(10, ErrorMessage = "Postcode cannot be longer than 10 characters.")]
public string Postcode { get; set; }
[Required]
[Display(Name = "Telephone")]
[StringLength(20, ErrorMessage = "Telephone cannot be longer than 20 characters.")]
public string Telephone { get; set; }
[StringLength(20, ErrorMessage = "Competition Licence Number cannot be longer than 20 characters.")]
[Display(Name = "Licence Number")]
public string CompLicenceNo { get; set; }
[Display(Name = "Licence Type")]
public int? SelectedLicenceTypeId { get; set; }
[StringLength(200, ErrorMessage = "Competition Licence Number cannot be longer than 20 characters.")]
[Display(Name = "Dietary Requirements - For events")]
public string Dietary { get; set; }
[Required]
[StringLength(100, ErrorMessage = "Next of Kin Name cannot be longer than 100 characters.")]
[Display(Name = "Next of kin First Name")]
public string NOKFirstName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "Next of Kin Name cannot be longer than 100 characters.")]
[Display(Name = "Next of kin Last Name")]
public string NOKLastName { get; set; }
[Required]
[StringLength(20, ErrorMessage = "Next of Kin Telephone cannot be longer than 20 characters.")]
[Display(Name = "Next of kin Telephone")]
public string NOKTelephone { get; set; }
[Display(Name = "Next of kin Relationship")]
public int? RelationshipTypeId { get; set; }
[Required]
[Display(Name = "Date of Birth")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/mm/yyyy}", ApplyFormatInEditMode = true)]
public DateTime DateOfBirth { get; set; }
[Display(Name = "Allow other organisations on Grass Roots Clicks to contact you?")]
public bool OtherOrgsGRC { get; set; }
[Display(Name = "Allow other clubs you are a member of or ones whose events you enter to contact you?")]
public bool OtherClubEvents { get; set; }
[Display(Name = "Allow other organisations outside of Grass Roots Clicks that we are working with to contact you?")]
public bool OtherOrgsOutside { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[EmailAddress]
[Display(Name = "Confirm email")]
[Compare("Email", ErrorMessage = "Your email and confirmation email do not match.")]
public string ConfirmEmail { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
View
#model GRCWebApp.Models.RegisterViewModel
#{
ViewBag.Title = "Register";
}
<h2 class="text-success">#ViewBag.Title</h2>
<div class="row">
<div class="col-md-7">
<div class="well bs-component">
<form class="form-horizontal">
<fieldset>
<section id="loginForm">
#using (Html.BeginForm("Register", "Account", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
#Html.AntiForgeryToken()
<hr />
#Html.ValidationSummary("", new { #class = "text-danger" })
<h3 class="text-success col-md-offset-1">Name & Address</h3>
<div class="form-group">
<div class="row">
<div class="col-md-3 col-md-offset-1">
#Html.LabelFor(model => model.FirstName, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { #class = "form-control", placeholder = "John" } })
#Html.ValidationMessageFor(model => model.FirstName, "", new { #class = "text-danger" })
</div>
<div class="col-md-4 col-md-offset-2">
#Html.LabelFor(model => model.LastName, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.LastName, new { htmlAttributes = new { #class = "form-control", placeholder = "Smith" } })
#Html.ValidationMessageFor(model => model.LastName, "", new { #class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-5 col-md-offset-1">
#Html.LabelFor(model => model.Address1, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.Address1, new { htmlAttributes = new { #class = "form-control", placeholder = "1 Apple Road" } })
#Html.ValidationMessageFor(model => model.Address1, "", new { #class = "text-danger" })
</div>
<div class="col-md-5">
#Html.LabelFor(model => model.Address2, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.Address2, new { htmlAttributes = new { #class = "form-control", placeholder = "Neighbourhood" } })
#Html.ValidationMessageFor(model => model.Address2, "", new { #class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-5 col-md-offset-1">
#Html.LabelFor(model => model.City, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.City, new { htmlAttributes = new { #class = "form-control", placeholder = "Some Town" } })
#Html.ValidationMessageFor(model => model.City, "", new { #class = "text-danger" })
</div>
<div class="col-md-5">
#Html.LabelFor(model => model.County, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.County, new { htmlAttributes = new { #class = "form-control", placeholder = "Someshire" } })
#Html.ValidationMessageFor(model => model.County, "", new { #class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-3 col-md-offset-1">
#Html.LabelFor(model => model.Postcode, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.Postcode, new { htmlAttributes = new { #class = "form-control", placeholder = "AA1 2BB" } })
#Html.ValidationMessageFor(model => model.Postcode, "", new { #class = "text-danger" })
</div>
</div>
</div>
<h3 class="text-success col-md-offset-1">Contact Details</h3>
<div class="form-group">
<div class="row">
<div class="col-md-4 col-md-offset-1">
#Html.LabelFor(model => model.Telephone, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.Telephone, new { htmlAttributes = new { #class = "form-control", placeholder = "01234 567890" } })
#Html.ValidationMessageFor(model => model.Telephone, "", new { #class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-4 col-md-offset-1">
#Html.LabelFor(model => model.Email, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #class = "form-control", placeholder = "me#provider.com" } })
#Html.ValidationMessageFor(model => model.Email, "", new { #class = "text-danger" })
</div>
<div class="col-md-4 col-md-offset-1">
#Html.LabelFor(model => model.ConfirmEmail, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.ConfirmEmail, new { htmlAttributes = new { #class = "form-control", placeholder = "me#provider.com" } })
#Html.ValidationMessageFor(model => model.ConfirmEmail, "", new { #class = "text-danger" })
</div>
</div>
</div>
<h3 class="text-success col-md-offset-1">Competition Licence</h3>
<div class="form-group">
<div class="row">
<div class="col-md-4 col-md-offset-1">
#Html.LabelFor(model => model.CompLicenceNo, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.CompLicenceNo, new { htmlAttributes = new { #class = "form-control", placeholder = "123456" } })
#Html.ValidationMessageFor(model => model.CompLicenceNo, "", new { #class = "text-danger" })
</div>
<div class="col-md-3 col-md-offset-1">
#Html.LabelFor(model => model.SelectedLicenceTypeId, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.SelectedLicenceTypeId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.SelectedLicenceTypeId, "", new { #class = "text-danger" })
</div>
</div>
</div>
<h3 class="text-success col-md-offset-1">Personal Details</h3>
<h4 class="text-success col-md-offset-1">Why do we need this information?</h4>
<p>To make it easier for you to enter events here on Grass Roots Clicks we gatther certain information that we can then populate into your entry. Why do we need a date of birth? Organisers gain great benefit form knowing what ages are taking part in their events. We dont share your date of birth, we use your current age to help the organisers and also tailor the entry form to you e.g. if your under 18 we'll ask you for parental permission for some events.</p>
<div class="form-group">
<div class="row">
<div class="col-md-3 col-md-offset-1">
#Html.LabelFor(model => model.DateOfBirth, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { #class = "form-control", placeholder = "01/12/80" } })
#Html.ValidationMessageFor(model => model.DateOfBirth, "", new { #class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-4 col-md-offset-1">
#Html.LabelFor(model => model.NOKFirstName, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.NOKFirstName, new { htmlAttributes = new { #class = "form-control", placeholder = "Jane" } })
#Html.ValidationMessageFor(model => model.NOKFirstName, "", new { #class = "text-danger" })
</div>
<div class="col-md-4 col-md-offset-1">
#Html.LabelFor(model => model.NOKLastName, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.NOKLastName, new { htmlAttributes = new { #class = "form-control", placeholder = "Smith" } })
#Html.ValidationMessageFor(model => model.NOKLastName, "", new { #class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-4 col-md-offset-1">
#Html.LabelFor(model => model.NOKTelephone, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.NOKTelephone, new { htmlAttributes = new { #class = "form-control", placeholder = "07234 567890" } })
#Html.ValidationMessageFor(model => model.NOKTelephone, "", new { #class = "text-danger" })
</div>
<div class="col-md-4 col-md-offset-1">
#Html.LabelFor(model => model.RelationshipTypeId, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.RelationshipTypeId, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.RelationshipTypeId, "", new { #class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-7 col-md-offset-1">
#Html.LabelFor(model => model.Dietary, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.Dietary, new { htmlAttributes = new { #class = "form-control", placeholder = "Vegetarian" } })
#Html.ValidationMessageFor(model => model.Dietary, "", new { #class = "text-danger" })
</div>
</div>
</div>
<h3 class="text-success col-md-offset-1">Password</h3>
<div class="form-group">
<div class="row">
<div class="col-md-4 col-md-offset-1">
#Html.LabelFor(model => model.Password, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.Password, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Password, "", new { #class = "text-danger" })
</div>
<div class="col-md-4 col-md-offset-1">
#Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { #class = "control-label" })
#Html.EditorFor(model => model.ConfirmPassword, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { #class = "text-danger" })
</div>
</div>
</div>
<h3 class="text-success col-md-offset-1">Contact</h3>
<div class="form-group">
<div class="row">
<div class="col-md-9 col-md-offset-1">
#Html.LabelFor(model => model.OtherOrgsGRC, htmlAttributes: new { #class = "control-label" })
</div>
<div class="col-md-1">
#Html.CheckBoxFor(model => model.OtherOrgsGRC, new { #checked = "checked" })
#Html.ValidationMessageFor(model => model.OtherOrgsGRC, "", new { #class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-9 col-md-offset-1">
#Html.LabelFor(model => model.OtherClubEvents, htmlAttributes: new { #class = "control-label" })
</div>
<div class="col-md-1">
#Html.CheckBoxFor(model => model.OtherClubEvents, new { #checked = "checked" })
#Html.ValidationMessageFor(model => model.OtherClubEvents, "", new { #class = "text-danger" })
</div>
</div>
<div class="row">
<div class="col-md-9 col-md-offset-1">
#Html.LabelFor(model => model.OtherOrgsOutside, htmlAttributes: new { #class = "control-label" })
</div>
<div class="col-md-1">
#Html.CheckBoxFor(model => model.OtherOrgsOutside, new { #checked = "checked" })
#Html.ValidationMessageFor(model => model.OtherOrgsOutside, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-1">
<input type="submit" value="Register" class="btn btn-success btn-lg" />
</div>
</div>
}
</section>
</fieldset>
</form>
</div>
</div>
<div class="col-md-4 panel panel-success">
<div class="panel-heading">
<h3 class="panel-title " align="center">Use another service to register</h3>
</div>
#Html.Partial("_ExternalLoginsListPartial", new GRCWebApp.Models.ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl })
</div>
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
#Scripts.Render("~/bundles/bootstrap")
<script type="text/javascript">
$("#DateOfBirth").datepicker({
format: "dd/mm/yyyy",
startDate: "-120y",
endDate: "-10y",
startView: 2,
calendarWeeks: true,
defaultViewDate: { year: 1975, month: 01, day: 01 }
});
</script>
}
I fixed the problem by removing the form, fieldset and section tags at the beginning of the view.
Thanks to ekad for helping debug the problem