【发布时间】:2012-03-09 20:11:26
【问题描述】:
当我的页面加载时,我的 ValidationSummary 和 ValidationMessageFor(model => model.TestNumbersString) 都显示验证错误,尽管几乎没有加载页面。我检查了 .css 文件是否在我的 _Layout.cshtml 中被引用,并且样式存在。
模型.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)
}
我注意到我的布局有许多 javascript 和 css 文件,但其他验证在页面上有效,并且我知道 site.css 已为页面的其他部分加载(但不是为我的表单,但这是另一个问题)。
_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" />
到目前为止,我所做的故障排除使我发现验证消息的类是 field-validation-error。所以我相信这不是 .css 错误(即使 css 样式应用不正确)。
我还想明确一点,这是一个部分布局。我不确定这在处理验证时是否会导致其他问题。
编辑:解决方案
感谢@jacqijvv 的解决方案。
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);
}
}
【问题讨论】:
标签: c# asp.net-mvc-3 razor validation