Copied to clipboard

Flag this post as spam?

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


  • Osman Coskun 164 posts 398 karma points
    Jan 05, 2018 @ 14:49
    Osman Coskun
    0

    Unobtrusive validation on page load in member update MVC view

    Hello,

    I developed a custom membership MVC code to enable visitors register the web site, validate the account, login and update their profile info.

    Everything works fine except the validation on member update page. When the page loads with the form input fields filled with the corresponding data of logged in member, i see validation alerts for the required fields.

    The model, view and controller code i developed is below. Any ideas about the alerts on page load, and how to fix it.

    Thanks in advance.

    The model

    public class MvcMemberUpdateModel
    {
        [HiddenInput(DisplayValue = false)]
        public int MemberID { get; set; }
    
        [Required]
        public string Name{ get; set; }
    
        [Required]
        public string Surname{ get; set; }
    
        [Required]
        [EmailAddress]
        public string Email { get; set; }
    
    
        [StringLength(12, MinimumLength = 6)]
        public string Password { get; set; }
    
        public string Company { get; set; }
    }
    

    The partial view

    using (Html.BeginUmbracoForm<MvcMemberSurfaceController>("HandleUpdateMember"))
    {
        var memberService = ApplicationContext.Current.Services.MemberService;
        var member = memberService.GetByEmail(Membership.GetUser().Email);
    
        @Html.AntiForgeryToken()
        @Html.ValidationSummary()
    
        <div class="form-horizontal">
    
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    
            <div class="form-group">
                @Html.DisplayNameFor(model => model.Name)
                <div class="col-md-10">
                    @Html.TextBoxFor(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.Surname, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Surname, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Surname, "", 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.Password, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.PasswordFor(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.Company, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.Company, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Company, "", new { @class = "text-danger" })
                    </div>
                </div>
    
    
            @Html.HiddenFor(model => model.MemberID)
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Update" class="btn btn-default" />
                </div>
            </div>
        </div>
    }
    

    The controller

    [Authorize]
        public ActionResult RenderEditProfileForm(MvcMemberUpdateModel model)
        {
            if (User.Identity.IsAuthenticated && Membership.GetUser() != null)
            {
                //MvcMemberUpdateModel model = new MvcMemberUpdateModel();
                var user = Membership.GetUser().UserName;
                if (user != null)
                {
                    var memberService = Services.MemberService;
                    var member = memberService.GetByEmail(user);
    
    
                    model.MemberID = member.Id;
                    model.Name= member.GetValue("name").ToString();
                    model.Surname= member.GetValue("surname").ToString();
                    model.Email = member.Email;
                    if (member.GetValue("Company")!=null) {model.Sirket = member.GetValue("Company").ToString();}
    
    
                    return PartialView("MvcEditProfileView", model);
                }
                else
                {
                    return PartialView("MvcEditProfileView", model);
                }
    
            }
            else
            {
                return PartialView("MvcEditProfileView", model);
            }
        }
    [HttpPost]
        public ActionResult HandleUpdateMember(MvcMemberUpdateModel model)
        {
            if (!ModelState.IsValid)
            {
                TempData["status"] = "Error on form";
                return CurrentUmbracoPage();
            }
            else
            {
    
                var member = Services.MemberService.GetById(model.MemberID);
                if (Services.MemberService.GetByEmail(model.Email) != null && (member.Username != model.Email))
                {
                    TempData["status"] = "member Exists";
                    return CurrentUmbracoPage();
                }
    
                member.Name = model.Name+ " " + model.Surname;
                member.Email = model.Email;
                member.Username = model.Email;
                member.SetValue("name", model.Name);
                member.SetValue("surname", model.Surname);
                member.SetValue("company", model.Company);
    
                //
                if (model.Password != null)
                {
                    Services.MemberService.SavePassword(member, model.Password);
                }
                Services.MemberService.Save(member);
    
                Members.Logout();
                FormsAuthentication.SetAuthCookie(model.Email, true);
                TempData["success"] = true;
    
                //Return the view
                TempData["status"] = "Member updated";
                return CurrentUmbracoPage();
    
    
            }
    
    
    
        }
    

    Update page template

    @Html.Action("RenderEditProfileForm", "MvcMemberSurface")
    
  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jan 05, 2018 @ 16:32
    Dan Diplo
    0

    I think when the member has been successfully updated in HandleUpdateMember (ie. no modelstate errors) then you need to use:

    return RedirectToCurrentUmbracoPage()
    

    rather than:

    return CurrentUmbracoPage()
    

    ie. Short version:

        if (!ModelState.IsValid)
        {
            return CurrentUmbracoPage();
        }
        else
        {
             return RedirectToCurrentUmbracoPage()
        }
    
  • Osman Coskun 164 posts 398 karma points
    Jan 06, 2018 @ 06:42
    Osman Coskun
    0

    Hi Dan,

    Thanks for the info. But this is not solving the problem.

  • Marcio Goularte 374 posts 1346 karma points
    Jan 06, 2018 @ 13:04
    Marcio Goularte
    0

    Hi,

    you have twice ValidationSummary, remove the @Html.ValidationSummary()

    and leave only @Html.ValidationSummary(true, "", new { @class = "text-danger" })

  • Osman Coskun 164 posts 398 karma points
    Jan 08, 2018 @ 10:10
    Osman Coskun
    0

    Hi Marcio,

    Thanks for pointing duplicate validation. But this also didn't solve the early validation issue.

Please Sign in or register to post replies

Write your reply to:

Draft