tr333
May 18th, 2011, 11:12 PM
If I have two separate forms on a view, each containing a single dropdownlist and submit button, what's the best way to pass the list values to the view so I can perform validation on each dropdownlist/form separately?
I've currently got a ViewModel with two IEnumerable<SelectListItem> properties (both with [Required]) for the lists and assigning that to @Html.DropDownList(), but I can't seem to get the associated @Html.ValidationMessageFor() to trigger when submitting the list with the default "-- Select --" value.
@using (Html.BeginForm("Action1", "Controller"))
{
@Html.AntiForgeryToken("Action1TokenSalt")
<fieldset>
<legend>List1</legend>
<div class="editor-field">
@Html.DropDownList("FooId", Model.List1Items, "-- Select --")
@Html.ValidationMessageFor(m => m.List1Items)
</div>
<p>
<input type="submit" value="Submit 1" />
</p>
</fieldset>
}
@using (Html.BeginForm("Action2", "Controller"))
{
@Html.AntiForgeryToken("Action2TokenSalt")
<fieldset>
<legend>List2</legend>
<div class="editor-field">
@Html.DropDownList("FooId", Model.List2Items, "-- Select --")
@Html.ValidationMessageFor(m => m.List2Items)
</div>
<p>
<input type="submit" value="Submit 2" />
</p>
</fieldset>
}
I've currently got a ViewModel with two IEnumerable<SelectListItem> properties (both with [Required]) for the lists and assigning that to @Html.DropDownList(), but I can't seem to get the associated @Html.ValidationMessageFor() to trigger when submitting the list with the default "-- Select --" value.
@using (Html.BeginForm("Action1", "Controller"))
{
@Html.AntiForgeryToken("Action1TokenSalt")
<fieldset>
<legend>List1</legend>
<div class="editor-field">
@Html.DropDownList("FooId", Model.List1Items, "-- Select --")
@Html.ValidationMessageFor(m => m.List1Items)
</div>
<p>
<input type="submit" value="Submit 1" />
</p>
</fieldset>
}
@using (Html.BeginForm("Action2", "Controller"))
{
@Html.AntiForgeryToken("Action2TokenSalt")
<fieldset>
<legend>List2</legend>
<div class="editor-field">
@Html.DropDownList("FooId", Model.List2Items, "-- Select --")
@Html.ValidationMessageFor(m => m.List2Items)
</div>
<p>
<input type="submit" value="Submit 2" />
</p>
</fieldset>
}