I set up a test with these models:
public class SimpleFormModel {
[Required]
public Guid aGuid { get; set; }
[Required]
public String aString { get; set; }
[Required]
public int anInt { get; set; }
public SimpleFormModel( ) {
this.aGuid = new Guid( "ABCD1234-ABCD-ABCD-ABCD-ABCD1234EF56" );
this.anInt = 42;
this.aString = "The string from the base constructor.";
}
}
public class AdditionalFormModel {
[Required]
public string aString2 { get; set; }
[Required]
public int anInt2 { get; set; }
}
and a controller with this action:
public ActionResult MultipleParameters( SimpleFormModel modelOne, AdditionalFormModel modelTwo ) {
if( !ModelState.IsValid ) {
modelOne.aString = "The model isn't valid.";
}
return View(modelOne);
}
and also this View:
@model SimpleFormModel
@{
ViewBag.Title = "MultipleParameters";
}
<h2>MultipleParameters</h2>
<form action="/Home/MultipleParameters" method="post">
<input name="aGuid" type="text" value="@Model.aGuid"/><br />
<input name="anInt" type="text" value="@Model.anInt"/><br />
<input name="aString" type="text" value="@Model.aString"/><br />
<br />
<button type="submit">Update</button>
</form>
No matter what, the string shows up in the form as "The model isn't valid.", as I'm not providing the required properties for AdditionalFormModel
. So, yes the model binder is checking the validity of both classes, even though the only indication that it has that an AdditionalFormModel
should be included is that class's presence in the Action's parameter list.
If I remove the [Required]
attributes from AdditionalFormModel
's properties, then the string is updated fine and the models are considered valid. Alternatively, I can provide an explicit parameterless constructor for AdditionalFormModel
that initializes each required property, like in SimpleFormModel
, and that works as well.
It would still be nice to see an answer with some insight into the process that results in this behavior, but this answers whether it checks both or not.