gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:42:01 25 4
gpt4 key购买 nike

当我的页面加载时,我的 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);
}

部分 View .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" />

到目前为止,我所做的故障排除让我发现验证消息的类是字段验证错误。所以我相信这不是 .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);
}
}

最佳答案

从 Action 方法调用中移除参数

例如

    public ActionResult Index()
{
TestModel model = new TestModel();
return View(model);
}

这应该可以解决问题

关于c# - 为什么我的 ValidationMessageFor For TextArea 在页面加载时显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9640331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com