Domanda

I have made a completely fresh MVC5 homepage and followed this tutorial: http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on

Instead of just adding 2 fields (HomeTown, BirthDate) i have added a lot of fields.

This is what i have modified in IdentityModel:

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int? GenderId { get; set; }
    public DateTime Birthday { get; set; }
    public string Email { get; set; }
    public int? Phone { get; set; }
    public string Homepage { get; set; }
    public string Image { get; set; }
    public string Address { get; set; }
    public int? ZipCode { get; set; }
    public string City { get; set; }
    public int? CountryId { get; set; }
    public DateTime? LastLogin { get; set; }
    public DateTime Created { get; set; }
    public bool Verified { get; set; }
}

This is what i have modified in AccountViewModels:

 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; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }

    [DisplayName("Gender")]
    [Required]
    public int? GenderId { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime Birthday { get; set; }

    [Required]
    [System.Web.Mvc.Remote("CheckEmail", "Account", ErrorMessage = "Email is already registered")]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Confirm email")]
    [Compare("Email", ErrorMessage = "The email and confirmation email do not match.")]
    public string ConfirmEmail { get; set; }
}

This is my register view:

@model MembershipTutorial.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(true)
<div class="form-group">
    @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.UserName)
    </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" })
        @Html.ValidationMessageFor(model => model.Password)
    </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" })
        @Html.ValidationMessageFor(model => model.ConfirmPassword)
    </div>
</div>


<div class="form-group">
    @Html.LabelFor(model => model.FirstName, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.FirstName)
        @Html.ValidationMessageFor(model => model.FirstName)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.LastName, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.LastName)
        @Html.ValidationMessageFor(model => model.LastName)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.GenderId, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(model => model.GenderId, (IEnumerable<SelectListItem>)ViewBag.GenderId, String.Empty)
        @Html.ValidationMessageFor(model => model.GenderId)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Birthday, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Birthday)
        @Html.ValidationMessageFor(model => model.Birthday)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Email, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        <div class="left-inner-addon">
            <i class="glyphicon glyphicon-envelope"></i>
            @Html.EditorFor(model => model.Email)
        </div>
        @Html.ValidationMessageFor(model => model.Email)
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(m => m.ConfirmEmail, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.EditorFor(m => m.ConfirmEmail, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.ConfirmEmail)
    </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")
    <link href="/Content/bootstrap-datepicker.css" rel="stylesheet" />
    <script src="/Scripts/bootstrap-datepicker.js"></script>
}

If i just click the submit button without entering anything it shows validation on everything. As i fill in the different fields the validation error messages goes away (as they should), but then when i hit submit. NOTHING HAPPENS. i have debugged it and set breakpoint at the start of:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)

but that never gets called. if i change the code back to 100% default then it gets called again. What did i mess up here? Wish it would just throw an exception in my face so i could see where the problem was.

È stato utile?

Soluzione

Did you wire up the System.Web.Mvc.Remote("CheckEmail", "Account"?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top