gpt4 book ai didi

asp.net-mvc - ValidationMessageFor 未显示错误

转载 作者:行者123 更新时间:2023-12-04 00:42:38 26 4
gpt4 key购买 nike

我有这门课:

public class Product
{
[Required(ErrorMessage = "empty name")]
public string Name { get; set; }

[Required(ErrorMessage = "empty description")]
public string Description { get; set; }
}

我有一个 ResponseProduct 显示在 View 中:
public class ProductInsertVM
{
public string Message = "Success";
public Product Product;
}

在我看来,我有这个:
@using (Html.BeginForm()){  
@Html.ValidationSummary(false)

@Html.TextBoxFor(m => m.Product.Description)
@Html.ValidationMessageFor(m => m.Product.Description)
}

我想知道的是为什么 ValidationMessageFor 不起作用? !!只有 ValidationSummary 有效。如果我用产品回应,那么它就起作用了。

最佳答案

如果满足以下条件,您提供的代码应该可以正常工作。

1) 您在 View 中引用了 jQuery 验证脚本

<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>

2) 在提交表单时,您正在调用 ModelState.IsValid 属性,从而验证传递给 action 方法的模型
[HttpPost]
public ActionResult CarList(CarList model)
{
if (ModelState.IsValid)
{
//Save or whatever you want to do
}
return View(model);
}

关于asp.net-mvc - ValidationMessageFor 未显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10693133/

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