gpt4 book ai didi

jquery - ASP.NET MVC 2 使用 EnableClientValidation() 时验证带有数据注释异常的强类型模型列表

转载 作者:行者123 更新时间:2023-12-01 06:08:18 30 4
gpt4 key购买 nike

这是用于将数据注释验证器添加到 Entity Framework 模型的伙伴类:

[MetadataType(typeof(Result_Validation))]
public partial class Result
{

}

[Bind(Include = "Title,Description")]
public class Result_Validation
{
[Required(ErrorMessage = "Title is required")]
public string Title { get; set; }

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

这是我的观点:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IList<Result>>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test</title>
</head>
<body>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<% for (int i = 0; i < 4; i++) { %>
<div class="editor-label">
<%: Html.LabelFor(model => model[i].Title)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model[i].Title)%>
<%: Html.ValidationMessageFor(model => model[i].Title)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model[i].Description)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model[i].Description)%>
<%: Html.ValidationMessageFor(model => model[i].Description)%>
</div>
<% } %>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
</body>
</html>

这对于服务器端验证来说效果很好,但是一旦我删除 Html.EnableClientValidation();在那里我收到此错误:“字典中不存在给定的键。”

..它指向 View 中的这一行:

<%: Html.ValidationMessageFor(model => model[i].Title)%>

最佳答案

尝试放置

<% Html.ValidationSummary(); %>

之前

<% Html.EnableClientValidation(); %>

关于jquery - ASP.NET MVC 2 使用 EnableClientValidation() 时验证带有数据注释异常的强类型模型列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3111771/

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