Reputation: 23
I have a view called Manage.cshtml
and inside this view there are two forms, one for changing password of the account and the other is for changing the mobile number of the user.
And there is one model for password and another for mobile number and one action method called manage (ViewModel model) I created a view model and put the password model and the mobile number model in it. The problem is that both models has required attributes. Now if the use wants to change only his mobile number it will not success because of validation since password fields are of required attributes.
What is the best way to separate the two models and being able to use them in the same view? I don't want use two separate views.
Upvotes: 1
Views: 1172
Reputation: 22619
You can call a RenderPartial and pass the model for each and enable the validation for each form independently
Main.cshml
@{ Html.RenderPartial("ChangePwd.cshtml", Model.PasswordModel); }
@{ Html.RenderPartial("MobileNo.cshtml", Model.MobileModel); }
ChangePwd.cshtml
@{Html.EnableClientValidation();}
@using (Html.BeginForm("ChangePwd", "Admin", FormMethod.Post))
{
}
Upvotes: 3
Reputation: 2170
Proper way is creating 2 separate views and merge them into single view using RenderView OR RenderAction
ChangePassword.cshtml
ChangeMobileNumber.cshtml
then create view called Manage.cshtml and refer above 2 views.
Upvotes: 0