I have a situation where I have a ViewModel that has a number of fields (As an example):
public class myVM
{
[Required(errorMessage="SomeMessage")]
public string Name;
[Required(errorMessage="SomeMessage")]
public string Address;
[Required(errorMessage="SomeMessage")]
public string PhoneNumber;
}
In the real world its a bit more complicated.
The user is given a choice of what they are on the page to edit, Name Info, Address Info, Phone Number Info etc.
Each of those options corresponds to a partial view that is loaded with the form for each section.
The issue that I have is that if a user goes on and selects that they want to just edit the Name section, so only the name form is rendered they still get the validation messages for the Address and Phone numbers...
I know WHY this is happening, just not sure what the best way to get round it is?
UPDATE.
Noted that the above is unclear, these form parts (Partial views) are all loaded into one BIG form.
Something like this :
@using (Html.BeginForm())
{
@Html.ValidationSummary(false)
Html.RenderPartial("Partials/_AddressForm", Model.Address);
Html.RenderPartial("Partials/_Phonenumber", Model.PhoneNumber);
<input type="submit" value="SAVE"/>
}
The resulting form is posted back into one action which does a ModelState.IsValid - Which of course validates the WHOLE model.