Each partial view returned above have a form posting to it's own controller action, which works fine, and client-side validation work as it should.
However when some server-side validation fail, the above (post) actions return that partial view:
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
......
}
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
return PartialView("_LogOn", model);
This obviously are the wrong way, since I will have the partial view rendered, but what I need is the parent form (Account/Index) with both partials and the validation error showing for the correct form.
Dang...I did this before, but just cannot remember now.
i guess (im not expert XD) even if with 2 different forms,model validations trigger as long as there is a submit button that was clicked. I guess, there is a way to exclude the other set of properties from binding when the other set of properties are posted.
I read somewhere before that the default validation system validates entire model on MVC version 2 and up.
krokonoster
Contributor
4291 Points
1352 Posts
Multiple forms per view - Server side validation
Jul 10, 2012 06:03 AM|LINK
Hi,
Did not use MVC a few months and got a bit rusty.
I got a LogOnViewModel and RegisterViewModel.
Account/Index return a view on which I have:
Each partial view returned above have a form posting to it's own controller action, which works fine, and client-side validation work as it should.
However when some server-side validation fail, the above (post) actions return that partial view:
if (ModelState.IsValid) { if (Membership.ValidateUser(model.UserName, model.Password)) { ...... } ModelState.AddModelError("", "The user name or password provided is incorrect."); } return PartialView("_LogOn", model);This obviously are the wrong way, since I will have the partial view rendered, but what I need is the parent form (Account/Index) with both partials and the validation error showing for the correct form.
Dang...I did this before, but just cannot remember now.
krokonoster
Contributor
4291 Points
1352 Posts
Re: Multiple forms per view - Server side validation
Jul 11, 2012 11:40 AM|LINK
Ok, I went a different way which works, other than my validation going coockoo.
Got a "LogOnViewModel" and a "RegisterViewModel" and created a composite view model "AuthenticationViewModel":
public class AuthenticationViewModel { public LogOnViewModel LogOnViewModel { get; set; } public RegisterViewModel RegisterViewModel { get; set; } }So my view looks like this:
@model xxx.Tasks.ViewModels.Account.AuthenticationViewModel @using (Html.BeginForm("LogOn", "Account")) { @Html.ValidationSummary() <div> @Html.LabelFor(m => m.LogOnViewModel.UserName) @Html.TextBoxFor(m => m.LogOnViewModel.UserName) @Html.ValidationMessageFor(m => m.LogOnViewModel.Password) ..... } @using (Html.BeginForm("Register", "Account")) { @Html.ValidationSummary() <div> @Html.LabelFor(m => m.RegisterViewModel.UserName) @Html.TextBoxFor(m => m.RegisterViewModel.UserName) @Html.ValidationMessageFor(m => m.RegisterViewModel.UserName) ..... } @section ViewScripts { <script src="@Url.Content("~/Assets/js/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Assets/js/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> }The seperate forms post to the correct action methods and if all fine, I'm allright.
However, validation giving me all kind of issues, such as:
All should be perfect if I can jsut figure out why things go wrong with my validation summaries and messages.
Anyone?
boybawang
Member
34 Points
7 Posts
Re: Multiple forms per view - Server side validation
Jul 11, 2012 03:38 PM|LINK
i guess (im not expert XD) even if with 2 different forms,model validations trigger as long as there is a submit button that was clicked. I guess, there is a way to exclude the other set of properties from binding when the other set of properties are posted. I read somewhere before that the default validation system validates entire model on MVC version 2 and up.
check out this post by Brad Wilson
http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html
[Bind(Exclude = "Property_Name")] maybe?
CPrakash82
All-Star
18720 Points
2899 Posts
Re: Multiple forms per view - Server side validation
Jul 12, 2012 02:06 AM|LINK
Here is an interesting discussion about the validation on multiple forms.
http://forums.asp.net/t/1710711.aspx/1
Thanks,