When my page loads my ValidationSummary and ValidationMessageFor(model => model.TestNumbersString) both display the validation error despite just barely loading the page. I have checked that the .css file is referenced in my _Layout.cshtml and the styles exist for it.
Model.cs
[Required]
public string TestNumbersString { get; set; }
HomeController.cs
public ActionResult Index(TestModel model)
{
return View(model);
}
partialView.cshtml
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "form" }))
{
@Html.TextAreaFor(model => model.TestNumbersString, new { @class = "testBox" })
@Html.ValidationMessageFor(model => model.TestNumbersString)
}
My Layout I noticed has a number of javascript and css files, but other validation works on the page, AND I know that the site.css is loaded for other parts of the page (but not for my form, but that’s another problem).
_Layout.cshtml
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
The troubleshooting I’ve done so far has lead me to find that the class for the validation message is field-validation-error. So I am lead to believe it’s not a .css error (even though the css styling isn’t applying correctly).
I also want to be clear that this is a partial layout. I’m not sure if this would cause additional problems when dealing with validation.
EDIT: Solution
Thanks to @jacqijvv for the solution.
public ActionResult Index()
{
TestModel model = new TestModel();
return View(model);
}
[HttpPost]
//We have these separate as this is the design for MVC.
public ActionResult Index(TDMixParametersViewModel model)
{
if (ModelState.IsValid)
return View(model);
else
{
model = new TDMixParametersViewModel();
return View(model);
}
}
Remove the parameter from the action method call
E.g.
That should fix the problem