验证消息未在客户端显示

时间:2016-04-22 12:28:05

标签: asp.net-mvc validation client-side-validation

这是我的模特

#! /bin/sh

# run-checks: run some checking command(s) on a proposed commit.
#
# Optionally, run it only on files that differ from those in
# the current commit (added or modified, treating rename as
# modify), and/or do not run it at all if there are
# no such files (e.g., if the commit consists only of file
# removals).

usage()
{
    echo "usage: $0 [-d] checkcmd [args ...]" 1>&2
    exit 1
}

# probably should use git rev-parse feature now, oh well
diffmode=false
skipempty=true
while true; do
    case "$1" in
    -d|--diff) diffmode=true; shift;;
    -z|--run-even-if-empty) skipempty=false; shift;;
    -dz) diffmode=true; skipempty=false; shift;;
    *) break;;
    esac
done

case "$#" in
0) usage;;
esac

# from here on, exit on error
set -e

# get temporary directory and arrange to clean it up
tdir=$(mktemp -d -t run-checks)
trap "rm -rf $tdir" 0 1 2 3 15

# Get list of changed files (whether or not we are using
# only the changed files).  This includes deleted files.
# For efficiency, we treat renames as delete/add pairs here.
# Require that new commit not match current commit.
if test $(git diff --cached --name-only --no-renames HEAD | wc -l) -eq 0; then
    echo "no changes to test before committing"
    exit 1
fi

# Populate work tree in temp dir.  If we only want changed
# files, limit the checkout to files added or modified.  Note
# that this list might be empty.
if $diffmode; then
    git diff --cached --name-only --no-renames --diff-filter=AM -z HEAD |
        xargs -0 git --work-tree=$tdir checkout -f --
else
    git --work-tree=$tdir checkout -f -- .
fi

# Now run checker in temp work tree.  Our exit status is
# its exit status.  Do not use exec since we must still clean
# up the temp dir, and optionally skip checker if work tree is empty.
cd $tdir
if test $(ls -A | wc -l) -eq 0; then
    is_empty=true
else
    is_empty=false
fi
if $skipempty && $is_empty; then exit 0; fi

if $is_empty; then
    $@
else
    $@ *
fi

在我看来

public partial class Asset
{
    public long ID { get; set; }
    [RegularExpression("^[0-9]*$", ErrorMessage = "Title must be numeric")]
    public string Title { get; set; }
    public string Description { get; set; }
}

问题是必需字段和正则表达式数字编号正在进行验证但正常表达式没有显示错误消息,因为我想显示该错误:标题必须是数字。 在应用验证时,请告诉我我在哪里做错了。

1 个答案:

答案 0 :(得分:1)

您未获得任何验证的原因是视图中的模型是

@model IEnumerable<Asset>

并为模型中不存在的属性生成输入

@Html.TextBox("Title")

创建<input name="Title" id = "Title" value="" />,但IEnumerable<Asset>没有名为Title的属性,因此不生成data-val-*属性,因此没有规则添加到$.validator到生成客户端验证。

请注意,您获得的唯一验证是添加new { required = "required" }属性的结果,该属性仅为HTML-5验证,并且不会为您提供必要的服务器端验证。

您可以通过创建视图模型来解决此问题

public class AssetVM
{
    public long? ID { get; set; }
    [Required(ErrorMessage = "Please enter a title")]
    [RegularExpression("^[0-9]*$", ErrorMessage = "Title must be numeric")]
    public string Title { get; set; }
    [Required(ErrorMessage = "Please enter a description")]
    public string Description { get; set; }
    public IEnumerable<Asset> Assets { get; set; }
}

并在控制器中初始化一个新的AssetVM并使用该集合填充Assets属性并将其返回到视图。

var model = new AssetVM()
{
    Assets = .... // your query
};
return View(model);

并在视图中

@model AssestVM
....
@using (Html.BeginForm("AssetsPage", "SuperAdmin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.HiddenFor(m => m.ID)
    ....
    @Html.TextBoxFor(m => m.Title, new { @class = "form-control"})
    @Html.ValidationMessage(m => m.Title)
    ....
}
....
@foreach(var asset in Model.Assets)
{
    // build your table
}

另一种方法是保留现有的@model IEnumerable<Asset>并创建一个部分视图,返回Asset的表单并在主视图中,然后使用@Html.Partial("_Asset", new Asset() )生成表单主要观点。

附注:

  1. 使用@Html.HiddenFor()生成ID的输入,而不是a textarea样式为隐藏
  2. 无需使用new { id = "###" } - HtmlHelper 方法已经添加了id属性,你刚刚覆盖了 具有相同值的值
  3. 删除new { required = "required" }属性使用 Unobtrusive Javascript而不是污染你的标记 行为
  4. 由于您还要上传文件,因此视图模型也应如此 包含属性public HttpPostedFileBase File { get; set; } 并以@Html.TextBoxFor(m => m.File, new { type = "file" })的形式。您还应该考虑文件显示名称的属性 这样就可以在视图中输出了
  5. 考虑将类属性添加到视图模型中 避免视图中的丑陋switch语句(在。中设置) 控制器)
  6. 删除操作应该是POST,而不是GET
  7. 有关如何实施此功能的实际示例,请参阅this DotNetFiddle,尽管在您的情况下,因为您还要上传文件,但您需要使用FormData发布表单,如上所述在this answer