View
@model List<Dal.IAdministrator>
@Html.Partial("PartialCreateAdmin", new ViewModels.AdministratorCreateModel())
Partial
@model ViewModels.AdministratorCreateModel
@using (Html.BeginForm("Insert", "Admin", FormMethod.Post, new { }))
{
<fieldset>
<br />
@Html.TextBoxFor(m => m.AccountName, new { placeholder = "Enter the account name here" })
@if (!ViewData.ModelState.IsValid)
{
<p style="color: darkblue;"> @ViewData.ModelState["AccountName"].Errors[0].ErrorMessage </p>
}
</fieldset>
<br />
<input type='Submit' value='Create' />
}
Controller:
[HttpPost]
public ActionResult Insert(AdministratorCreateModel vm)
{
// ...
if(!accountNameExists)
{
ModelState.AddModelError("AccountName", "Account name does not exist, please try again!");
return PartialView("PartialCreateAdmin", vm);
}
// ...
}
Currently If the validation fails, it returns to the partial, but I want to return the view, with the message along the form? How can I do this?