I'm not quite understanding how this works.
Passing parameters from my entity objects works fine. But when I create new fields, only the first one is retrieved.
Model User Class:
public class User {
[Key]
public long Uid { get; set; }
[Required]
[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email:")]
public string Email { get; set; }
[Required]
[StringLength(20, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
[Display(Name = "User Name:")]
public string Username { get; set; }
public string Password { get; set; }
public byte Role { get; set; }
public DateTime Created { get; set; }
}
CSHTML:
#using (Html.BeginForm( null,
null,
FormMethod.Post,
new { id = "regform" })
) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Register</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Email)
#Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Username)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Username)
#Html.ValidationMessageFor(model => model.Username)
</div>
<div class="editor-label">
Password:
</div>
<div class="editor-field">
#Html.Password("pwd")
</div>
<div class="editor-label">
Confirm Password:
</div>
<div class="editor-field">
#Html.Password("confirm")
</div>
<p>
<input type="submit" value="Register" />
</p>
</fieldset>
}
Controller:
[HttpPost]
public ActionResult Register(User user, string pwd, string confirm) {
user.Username = confirm;
user.Created = DateTime.Now;
user.Role = 255;
user.Password = EncryptPassword.Password(pwd);
if (ModelState.IsValid && pwd == confirm) {
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
Where I'm getting confused, is pwd picks up fine. confirm on the other hand remains null. My initial thought that it was calling by order and confirm in the model was simply conPwd. When that didn't work, I changed it's name to confirm. It still is not working and I can't find anything that explains how multiple parameters are passed to the controller.
Edit:
Updated my code. Believe it or not, this alone has taken me most of the day to write because I've been trying to understand what I'm doing. There is just so much to take in when you're learning Entities, LINQ, MVC, ASP.NET and Razor all at the same time. Basic C# is the only part I came in to this knowing. :)
You need a strongly typed view for your RegisterModel then use a Html.BeginForm to post the data to the controller.
Model
// This is the Model that you will use to register users
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email { 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 (CSHTML)
// This is your strongly typed view that will use
// model binding to bind the properties of RegisterModel
// to the View.
#model Trainer.Models.RegisterModel
// You can find these scripts in default projects in Visual Studio, if you are
// not using VS, then you can still find them online
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
// This is where your form starts
// The "Account" parameter states what controller to post the form to
#using (Html.BeginForm((string)ViewBag.FormAction, "Account")) {
#Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")
<fieldset>
<legend>Registration Form</legend>
<ol>
<li>
#Html.LabelFor(m => m.UserName)
#Html.TextBoxFor(m => m.UserName)
#Html.ValidationMessageFor(m => m.UserName)
</li>
<li>
#Html.LabelFor(m => m.Email)
#Html.TextBoxFor(m => m.Email)
#Html.ValidationMessageFor(m => m.Email)
</li>
<li>
#Html.LabelFor(m => m.Password)
#Html.PasswordFor(m => m.Password)
#Html.ValidationMessageFor(m => m.Password)
</li>
<li>
#Html.LabelFor(m => m.ConfirmPassword)
#Html.PasswordFor(m => m.ConfirmPassword)
#Html.ValidationMessageFor(m => m.ConfirmPassword)
</li>
</ol>
<!-- The value property being set to register tells the form
what method of the controller to post to -->
<input type="submit" value="Register" />
</fieldset>
}
Controller
// The AccountController has methods that only authorized
// users should be able to access. However, we can override
// this with another attribute for methods that anyone
// can access
[Authorize]
public class AccountController : Controller
{
// This will allow the View to be rendered
[AllowAnonymous]
public ActionResult Register()
{
return ContextDependentView();
}
// This is one of the methods that anyone can access
// Your Html.BeginForm will post to this method and
// process what you posted.
[AllowAnonymous]
[HttpPost]
public ActionResult Register(RegisterModel model)
{
// If all of the information in the model is valid
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus);
// If the out parameter createStatus gives us a successful code
// Log the user in
if (createStatus == MembershipCreateStatus.Success)
{
FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
return RedirectToAction("Index", "Home");
}
else // If the out parameter fails
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
}
Related
I am having a problem referencing a table that is using Entity Frameworks in a View Model.
This is the Model, I'm not really sure why the class is being called a ViewModel but I didn't name it.
public class RegisterViewModel
{
[Required]
[Display(Name = "User name")]
public string UserName { 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; }
[Required]
[Display(Name = "Email")]
public string Email { get; set; }
}
I have a very simple View Model Referencing this model, I think:
public class UserCreateNewViewModel
{
public RegisterViewModel Register = new RegisterViewModel();
}
In my Controller I have:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UserCreateNew(RegisterViewModel model)
{
var m = new UserCreateNewViewModel();
if (ModelState.IsValid)
{
string registerToken =
WebMatrix.WebData.WebSecurity.CreateUserAndAccount(model.UserName,
model.Password, new { Email = model.Email }, true);
SimpleSecurity.WebSecurity.ConfirmAccount(registerToken);
string confirmationToken =
WebMatrix.WebData.WebSecurity.GeneratePasswordResetToken(model.UserName);
dynamic email = new Email("ChngPasswordEmail");
email.To = model.Email;
email.UserName = model.UserName;
email.baseUrl = GetBaseUrl();
email.ConfirmationToken = confirmationToken;
email.Send();
}
return RedirectToAction("UserList");
}
I think its weird that I am trying to pass in RegisterViewModel as a parameter but then trying to instantiate m as UserCreateViewModel() But I am not sure how to get my registerViewModel into the view via UserCreateViewModel.
View:
#model ComtrexCloudReporting.Models.UserCreateNewViewModel
#{
ViewBag.Title = "UserCreate";
}
<div class="spacerBody">
<h2 class="admin-home-link orange-titles orange-titles-large">#Html.ActionLink("Create Users", "AdminIndex")</h2>
<div class="to-link navlinks"> #Html.ActionLink("Users", "UserList") | #Html.ActionLink("Manage User Role", "RoleManageUser")</div>
<p> </p>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Registration Form</legend>
<ol class="comtrexBlue-text">
<li>
#Html.LabelFor(m => m.UserName)
#Html.TextBoxFor(m => m.UserName)
</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.Email)
#Html.TextBoxFor(m => m.Email)
</li>
</ol>
<button class="btn larger" type="submit">Create</button>
</fieldset>
}````
I'm trying to make a login page but my modelstate is coming back as invalid, i assume it's because i'm getting an "incomplete" object back.
here is my controller
public ActionResult Login()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(User u)
{
if (ModelState.IsValid)
{
{
var v = entities.Users.Where(a => a.UserName.Equals(u.UserName) && a.Password.Equals(u.Password)).FirstOrDefault();
if (v != null)
{
Session["LoggedUserID"] = v.UserID.ToString();
Session["LoggedUserFullname"] = v.FirstName +" "+ v.LastName;
return RedirectToAction("AfterLogin");
}
}
}
return View(u);
}
here is the Login page itself
#model Car_Dealership.Views.Shared.User
#{
ViewBag.Title = "Login";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Login</h2>
#using (Html.BeginForm("Login", "Home", FormMethod.Post))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
if (#ViewBag.Message != null)
{
<div style="border:1px solid red">
#ViewBag.Message
</div>
}
<div>
<fieldset>
<legend>Login</legend>
<div class="editor-label">
#Html.LabelFor(u => u.UserName)
</div>
<div class="editor-field">
#Html.TextBoxFor(u => u.UserName)
#Html.ValidationMessageFor(u => u.UserName)
</div>
<div class="editor-label">
#Html.LabelFor(u => u.Password)
</div>
<div class="editor-field">
#Html.PasswordFor(u => u.Password)
#Html.ValidationMessageFor(u => u.Password)
</div>
<input type="submit" value="Login" />
</fieldset>
</div>
}
#* This below line is for create javascript section *#
#section Scripts{
#Scripts.Render("~/bundles/jqueryval")
}
and here is User
namespace Car_Dealership.Views.Shared
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class User
{
public User()
{
this.Orders = new HashSet<Order>();
}
[Required]
public int UserID { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required(ErrorMessage ="Date Of Birth is Required in YYYY-MM-DD Format")]
[Display(Name = "Date Of Birth")]
[RegularExpression(#"/^\d{4}-\d{2}-\d{2}$/")]
public System.DateTime DateOfBirth { get; set; }
[Required]
public string Gender { get; set; }
[Required(ErrorMessage = "Email is Required")]
[RegularExpression(#"^([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}" +
#"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
#".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
ErrorMessage = "Email is not valid")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
public string Authorize { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
I've tried making u have all the properties of the object it's supposed to correspond to by doing if(u.username=entities.users.where(a=>a.username)&& the same for password) but that doesn't work. not really sure what to do here
Your assumption is correct: User object that is passed from the client is not full and misses required properties.
A solution:
Your Login view should not receive a User model instead create a LoginViewModel:
public class LoginViewModel
{
[Required]
[Display(Name = "User name")]
public string UserName {get; set;}
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
public string Password {get; set;}
}
And your view would be:
#model LoginViewModel
#using (Html.BeginForm("Login", "Home", FormMethod.Post))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
if (#ViewBag.Message != null)
{
<div style="border:1px solid red">
#ViewBag.Message
</div>
}
<div>
<fieldset>
<legend>Login</legend>
<div class="editor-label">
#Html.LabelFor(u => u.UserName)
</div>
<div class="editor-field">
#Html.TextBoxFor(u => u.UserName)
#Html.ValidationMessageFor(u => u.UserName)
</div>
<div class="editor-label">
#Html.LabelFor(u => u.Password)
</div>
<div class="editor-field">
#Html.PasswordFor(u => u.Password)
#Html.ValidationMessageFor(u => u.Password)
</div>
<input type="submit" value="Login" />
</fieldset>
</div>
}
Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel u)
{
if (ModelState.IsValid)
{
{
var v = entities.Users.Where(a => a.UserName.Equals(u.UserName) && a.Password.Equals(u.Password)).FirstOrDefault();
if (v != null)
{
Session["LoggedUserID"] = v.UserID.ToString();
Session["LoggedUserFullname"] = v.FirstName +" "+ v.LastName;
return RedirectToAction("AfterLogin");
}
}
}
return View(u);
}
P.S. storing plain passwords is REALLY bad idea. You should store password hashes instead. I would also advise that you take a look at [asp.net identity1 it really can make user management much easier.
I am new to ASP.NET MVC and using version 5. I created a form that is in the layout, and I cannot cannot get it to show validation errors on the view. It will post to the action correctly, and if the model is valid, it will execute. If the model is invalid I will get the following error.
I am hoping someone can point me in the right direction. Thank you in advance!
Server Error in '/' Application.
The view 'ContactSubmit' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/ContactSubmit.aspx
~/Views/Home/ContactSubmit.ascx
~/Views/Shared/ContactSubmit.aspx
~/Views/Shared/ContactSubmit.ascx
~/Views/Home/ContactSubmit.cshtml
~/Views/Home/ContactSubmit.vbhtml
~/Views/Shared/ContactSubmit.cshtml
~/Views/Shared/ContactSubmit.vbhtml
This is my model I am using:
public partial class Lead
{
[Key]
public int LeadId { get; set; }
[Required]
[StringLength(50, MinimumLength=2, ErrorMessage="* A valid first name is required.")]
[Display(Name="First Name")]
public string FirstName { get; set; }
[Required]
[StringLength(50, MinimumLength=2, ErrorMessage="* A valid last name is required.")]
[Display(Name="Last Name")]
public string LastName { get; set; }
[Required]
[StringLength(50, MinimumLength=2, ErrorMessage="* A valid company is required.")]
public string Company { get; set; }
[Required]
[StringLength(50)]
[EmailAddress(ErrorMessage="* A valid email address is required.")]
public string Email { get; set; }
[Required]
[StringLength(15, MinimumLength=9, ErrorMessage="* A valid phone nunber is required.")]
[Phone(ErrorMessage="Please enter a valid phone number.")]
public string Phone { get; set; }
}
This is the code I have in my Home controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ContactSubmit(
[Bind(Include = "FirstName, LastName, Company, Email, Phone")]
Lead lead)
{
try
{
if (ModelState.IsValid)
{
lead.Tenant = SessionManager.Get<Tenant>(Constants.SessionTenant);
lead.Refferer = SessionManager.Get<string>(Constants.SessionRefferal);
DataStoreManager.AddLead(lead);
return RedirectToAction("SubmissionConfirmed", lead);
}
}
catch (DataException /* dex */)
{
ModelState.AddModelError("", "Unable to perform action. Please contact us.");
return RedirectToAction("SubmissionFailed", lead);
}
return View(lead);
}
[HttpGet]
public ActionResult ContactSubmit()
{
return View();
}
This is the form that I have in my layout:
#using (Html.BeginForm("ContactSubmit", "Home", FormMethod.Post))
{
#Html.AntiForgeryToken()
<fieldset>
<div class="editor-label">
#Html.LabelFor(m => m.FirstName)
</div>
<div class="editor-field">
#Html.EditorFor(m => m.FirstName)
#Html.ValidationMessageFor(m => m.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(m => m.LastName)
</div>
<div class="editor-field">
#Html.EditorFor(m => m.LastName)
#Html.ValidationMessageFor(m => m.LastName)
</div>
<div class="editor-label">
#Html.LabelFor(m => m.Company)
</div>
<div class="editor-field">
#Html.EditorFor(m => m.Company)
#Html.ValidationMessageFor(m => m.Company)
</div>
<div class="editor-label">
#Html.LabelFor(m => m.Email)
</div>
<div class="editor-field">
#Html.EditorFor(m => m.Email)
#Html.ValidationMessageFor(m => m.Email)
</div>
<div class="editor-label">
#Html.LabelFor(m => m.Phone)
</div>
<div class="editor-field">
#Html.EditorFor(m => m.Phone)
#Html.ValidationMessageFor(m => m.Phone)
</div>
<div class="masthead-button-wrapper">
<input class="btn btn-warning" type="submit" value="Submit" />
</div>
</fieldset>
}
There is one error in your code, I didn't notice first. In the get method you are using -
return View();
Which means your view does not allow parameter but when there is an error you are using -
return View(lead);
In this case MVC is looking for the view with the same name but which also accepts a parameter of Lead type and it fails since there is no view with that option and the only one that is found does not accept parameter as seen from the Get method. When there is no error, you are redirecting to -
return RedirectToAction("SubmissionConfirmed", lead);
and the View with the parameter is never needed to be searched and thus no error.
So, change the view to accept a parameter of Lead and change your get method accordingly.
May be this would help. -
[HttpGet]
public ActionResult ContactSubmit()
{
var lead = new Lead();
return View(lead);
}
and in the view add
#model Lead
at the top
EDIT : In case since you are redirecting you should know that ModelState gets initialized in each request, so redirecting clears it automatically. You have to use some other means to pass modelstate or better if you use client side validation.
I am using the ASP.NET web application template and trying to allow a user to pick a role when registering.
Here is what I got at the moment.
Does it
View
<fieldset class="col-lg-5 .col-md-5">
<legend>Registration Form</legend>
<p>
#Html.LabelFor(m => m.UserName)
#Html.TextBoxFor(m => m.UserName, new { #class = "form-control" })
</p>
<p>
#Html.LabelFor(m => m.Password)
#Html.PasswordFor(m => m.Password, new { #class = "form-control" })
</p>
<p>
#Html.LabelFor(m => m.ConfirmPassword)
#Html.PasswordFor(m => m.ConfirmPassword, new { #class = "form-control" })
</p>
<p>
#Html.DropDownListFor(model => model.Type, Model.TypeList)
</p>
<input type="submit" value="Register" class="btn btn-default" />
</fieldset>
Model
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { 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; }
public string Type { get; set; }
public IEnumerable<SelectListItem> TypeList
{
get
{
return new List<SelectListItem>
{
new SelectListItem { Text = "athlete", Value = "athlete"},
new SelectListItem { Text = "coach", Value = "coach"},
};
}
}
}
Controller
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
Roles.AddUserToRole(model.UserName, model.Type);
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
I am feel like I am close but I can't quiet get it and i am getting this error
Compiler Error Message: Models.RegisterModel>' does not contain a definition for 'DropDownListFor' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.DropDownListFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>, System.Collections.Generic.IEnumerable)' has some invalid arguments
Did you initialize your model in HttpGet Register method? Like below...
[AllowAnonymous]
public ActionResult Register()
{
var model = new RegisterModel();
return View(model);
}
I create an empty template MVC4 app, added your code, got object reference not set error as default Register model does not pass model object to view which you are trying to access (i.e. loading TypeInt in DropDownListFor()).
I then initialized model in Get method as shown above. All works fine, i was able to pick a role on register view.
Check if this helps.
take a look your last comma should be removed:
new SelectListItem { Text = "athlete", Value = "athlete"},
new SelectListItem { Text = "coach", Value = "coach"}
I'm having two issues with editing my applications RegisterModel.
A) The fields UserName and Email are rendered as password fields?
B) The modelstate is always invalid (and my model is empty)
I think they are both caused because I have a "HomeModel" which contains "LoginModel" and "RegisterModel" property and it passes the entire HomeModel instead of the corresponding property. How can I make it pass the correct one?
I have the following form:
#using (Ajax.BeginForm("Register", "Account", new AjaxOptions { UpdateTargetId = "RegisterAjaxResponse" }))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<div class="form-row">
<div id="RegisterAjaxResponse"></div>
</div>
<div class="form-row">
#Html.LabelFor(m => m.RegisterModel.UserName)
#Html.PasswordFor(m => m.RegisterModel.UserName)
#Html.ValidationMessageFor(m => m.RegisterModel.UserName)
</div>
<div class="form-row">
#Html.LabelFor(m => m.RegisterModel.Password)
#Html.PasswordFor(m => m.RegisterModel.Password)
#Html.ValidationMessageFor(m => m.RegisterModel.Password)
</div>
<div class="form-row">
#Html.LabelFor(m => m.RegisterModel.ConfirmPassword)
#Html.PasswordFor(m => m.RegisterModel.ConfirmPassword)
#Html.ValidationMessageFor(m => m.RegisterModel.ConfirmPassword)
</div>
<div class="form-row">
#Html.LabelFor(m => m.RegisterModel.Email)
#Html.PasswordFor(m => m.RegisterModel.Email)
#Html.ValidationMessageFor(m => m.RegisterModel.Email)
</div>
<div class="form-row">
<input type="submit" value='Register' />
</div>
}
The model:
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
[DataType(DataType.Text)]
public string UserName { 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; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "EmailAddress")]
public string Email { get; set; }
}
But the UserName and Email field are rendered as an password field.
http://i.imgur.com/GCamint.png
-Can't page images yet, sorry.
And my modelstate is always invalid.
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
string returnValue = "";
if (ModelState.IsValid)
{
//Some code that is never executed
}
return Content(returnValue, "text/html");
}
Problem A: you're rendering the textfields for email and username using #Html.PasswordFor(), this will render password fields, try using #Html.TextboxFor()
And for problem B, it depends if you're targetting MVC3 or 4 and which version of .NET.
Later versions of .NET use the compare annotation as
[Compare(CompareField = Password, ErrorMessage = "Passwords do not
match")]
a) Because you have in razor view for them
#Html.PasswordFor(m => m.RegisterModel.UserName)
need to be
#Html.TextboxFor(m => m.RegisterModel.Email)