ModelState.AddModelError到字段的特定ValidationMessageFor? [英] ModelState.AddModelError to a particular ValidationMessageFor of a field?

查看:66
本文介绍了ModelState.AddModelError到字段的特定ValidationMessageFor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来生成html输入和验证消息.

I have the following code to generate an html input and validation message.

@Html.ValidationSummary(false)
......
<div class="col-md-10">
    @Html.TextBoxFor(model => model.ImageUpload, new { type = "file", name = "file" })
    @Html.ValidationMessageFor(model => model.ImageUpload)
</div>

在我的操作中,我有代码

In my action I have the code

if (.... something wrong with the input ....) 
{
    ModelState.AddModelError("", "Invalid image file.");
    return RedirectToAction(....

但是,它将在验证摘要"部分中显示错误消息.是否也可以在验证消息部分中显示错误消息以供输入?

However, it will show an error message in the validation summary section. Is it possible to show the error message in the validation message section for input too?

推荐答案

您需要提供一个密钥:

ModelState.AddModelError("ImageUpload", "Invalid image file.");

这篇关于ModelState.AddModelError到字段的特定ValidationMessageFor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆