Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Roger Withnell 128 posts 613 karma points
    Jun 23, 2016 @ 09:53
    Roger Withnell
    0

    Perpetuate form content on server side validation error

    I have built a form for site visitors to register as a Member. They enter Name, Email (also used as Username and Login) and Password and the code creates the member records. This works fine.

    Where I'm stuck is when the email entered already exists. I check for this with

    if (Services.MemberService.Exists(vEmail))
    

    which returns me to the page with the form. But the form is empty and I want to populate it with the information already entered.

    How do I do this? Here are the relevant bits of the code:

    Model

    public class MemberRegistrationModel
    {
        [Required]
        public string Name { get; set; }
    
        [Required]
        [EmailAddress]`enter code here`
        public string Email { get; set; }
    

    Controller

       [HttpPost]
        public ActionResult HandleFormSubmit(MemberRegistrationModel model)
        {
            if (!ModelState.IsValid)
                return RedirectToCurrentUmbracoPage();
            string vDomainName = model.domainName;
            string vName = model.Name.Trim();
            string vEmail = model.Email.Trim();
            string vPassword = model.Password;
    
            //Check if the email address already exists.
            if (Services.MemberService.Exists(vEmail))
                return RedirectToCurrentUmbracoPage();
    

    Partial View

    @model UmbracoWebsites.Models.MemberRegistrationModel
    
    @if (TempData["success"] == null)
    {
        using (Html.BeginUmbracoForm<UmbracoWebsites.Controllers.MemberRegistrationSurfaceController>("HandleFormSubmit"))
        {
            <div class="form-horizontal" role="form">
                <div class="form-group">
                    <label class="col-md-4 control-label" for="Name">Name:</label>
                    <div class="col-md-8">
                        @Html.TextBoxFor(m => m.Name, new { id = "Name", @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Name)
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-4 control-label" for="Email">Email:</label>
                    <div class="col-md-8">
                        @Html.TextBoxFor(m => m.Email, new { id = "Email", @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Email)
                    </div>
                </div>
    

    Your help would be much appreciated.

    Thanking you in anticipation.

    Roger

  • Roger Withnell 128 posts 613 karma points
    Jul 06, 2016 @ 14:41
    Roger Withnell
    100

    I eventually fixed this by using a session variable

Please Sign in or register to post replies

Write your reply to:

Draft