为什么我的ValidationMessageFor For TextArea在页面加载时显示

Qui*_*orn 2 c# validation razor asp.net-mvc-3

当我的页面加载我的ValidationSummary和ValidationMessageFor(model => model.TestNumbersString)时,尽管只是几乎没有加载页面,但都显示验证错误.我已经检查过.css文件是在我的_Layout.cshtml中引用的,并且它的样式是存在的.

Model.cs

[Required]
public string TestNumbersString { get; set; }
Run Code Online (Sandbox Code Playgroud)

HomeController.cs

public ActionResult Index(TestModel model)
{
    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

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)
}
Run Code Online (Sandbox Code Playgroud)

我注意到我的布局有很多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" />
Run Code Online (Sandbox Code Playgroud)

到目前为止我所做的故障排除让我发现验证消息的类是字段验证错误.因此我认为它不是.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);
    }
}
Run Code Online (Sandbox Code Playgroud)

jac*_*jvv 6

从操作方法调用中删除参数

例如

    public ActionResult Index()
    {
        TestModel model = new TestModel();
        return View(model);
    }
Run Code Online (Sandbox Code Playgroud)

这应该解决问题