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();
}
}
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
The partial view
The controller
Update page template
I think when the member has been successfully updated in HandleUpdateMember (ie. no modelstate errors) then you need to use:
rather than:
ie. Short version:
Hi Dan,
Thanks for the info. But this is not solving the problem.
Hi,
you have twice ValidationSummary, remove the @Html.ValidationSummary()
and leave only @Html.ValidationSummary(true, "", new { @class = "text-danger" })
Hi Marcio,
Thanks for pointing duplicate validation. But this also didn't solve the early validation issue.
is working on a reply...