[英]returning modelState error message
I have a controller action where I perform some validations. 我有一个控制器动作,在其中执行一些验证。 I want to use ModelState.AddModelError to summarize the errors and display all at once, but without creating a view and using return content() instead. 我想使用ModelState.AddModelError汇总错误并一次显示所有错误,但不创建视图并使用return content()代替。 Is this possible? 这可能吗? Here is my code so far: 到目前为止,这是我的代码:
public ActionResult Validate(string lName, string fName)
{
var lName= DataContext.LName(lName);
var fName = DataContext.FName(fName);
if (lName == null)
ModelState.AddModelError("lName", "Last Name " + lName + " not found);
if (fName== null)
ModelState.AddModelError("fName", "First Name " + fName + " not found);
return Content("Display Error Summary");
}
You are adding the errors to MdoelState but you may need to test the validity also like so: 您正在将错误添加到MdoelState,但是您可能还需要像这样测试有效性:
if (!ModelState.IsValid)
{
return View();//Show error
}
else
{
return redirectToAction("Index")// No errors so move on
}
And then in your Razor veiw 然后以您的剃刀面纱
@Html.ValidationSummary(true)
You've asked this question before: returning custom error message for multiple variables 您之前曾问过这个问题: 返回多个变量的自定义错误消息
You are attempting to mix two different ideas. 您正在尝试混合两种不同的想法。 I would recommend following LillyPop's answer or using the answer you selected from your earlier question. 我建议您遵循LillyPop的答案,或使用从先前问题中选择的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.