Frage

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.

War es hilfreich?

Lösung

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))
 {


 }

Andere Tipps

Proper way is creating 2 separate views and merge them into single view using RenderView OR RenderAction

Create following views:

ChangePassword.cshtml

ChangeMobileNumber.cshtml

then create view called Manage.cshtml and refer above 2 views.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top