在ASP.NET Core中,可以使用标签助手替换
@Html.ValidationMessage
。
以下是一个示例代码:
在视图中:
@model YourModel
在模型中:
public class YourModel
{
[Required(ErrorMessage = "Field Name is required")]
public string FieldName { get; set; }
}
在控制器中的POST动作中,需要添加模型验证:
[HttpPost]
public IActionResult YourAction(YourModel model)
{
if (ModelState.IsValid)
{
// 处理逻辑
return RedirectToAction("Success");
}
return View(model);
}
这样,当表单提交时,如果FieldName
字段验证失败,将会显示错误消息。