- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的数据模型:
public class Team
{
[Key]
public int Id { get; set;}
[Required]
public string Name { get; set; }
[MinLength(1)]
public virtual ICollection<User> Users { get; set; }
}
我的问题是,当我稍后尝试创建一个新团队(有一个用户)时,我在保存上下文时遇到以下问题。
An unexpected exception was thrown during validation of 'Users' when invoking System.ComponentModel.DataAnnotations.MinLengthAttribute.IsValid. See the inner exception for details.
内部异常如下:
{"Unable to cast object of type 'System.Collections.Generic.List`1[MyNameSpace.Model.User]' to type 'System.Array'."}
这是实际保存的代码(目前位于 Controller 中):
if (ModelState.IsValid)
{
team.Users = new List<User>();
team.Users.Add(CurrentUser);//CurrentUser is a property that gives me the currently active User (MyNamespace.Model.User).
DB.Teams.Add(team);//DB is a DbContext object that holds DbSets of all my models
DB.SaveChanges();
return RedirectToAction("Index");
}
那么,这是怎么回事?是我做错了什么,还是发生了其他事情?
最佳答案
我不相信您能够使用 MinLength 属性来实现您想要实现的目标。 Here是 MinLength 属性的 msdn 页面。根据描述:“指定属性中允许的字符串数据数组的最小长度。”因此,如您所见,它只能用于字符串数据数组。您可能需要创建自己的自定义 ValidationAttribute 来处理您的场景。
关于casting - ICollection 上的 MinLength 约束失败, Entity Framework ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11566265/
我正在尝试在我的 symfony 项目中使用 MinLength 验证器。 下面是我的使用方法: use Symfony\Component\Validator\Constraints\MinLeng
我正在尝试创建一个自动完成功能,由于结果集的大小,当输入的字符少于 3 个时不会触发搜索。然而,有些名字实际上只有 1 或 2 个字母。 解决方案是允许用户单击输入中的放大镜来提交,或“强制通过”具有
我正在使用 jquery 自动完成插件。配置为使用多个值。 我设置选项minLength:2 minLength 选项仅适用于第一个自动建议单词,对于多个值则不起作用。 我如何配置或需要重写哪种方法来
这是我在 stackoverflow 上发表的第一篇文章,该网站给我留下了深刻的印象! 我的问题是关于 jQuery 验证插件...特别是关于 minlength 方法。我有一组复选框,我想知道是否至
我将 minLength 设置为 3,以在输入至少 3 个字符时触发建议列表。 但是当输入 1 个字符时,下拉菜单会打开。如何让它工作? $('#remote .typeahead').typeahe
我是 knockout 的新手,正在尝试锻炼如何使用验证。我们有以下代码 - var MyObjectModel = function(myObject){ var self = th
我正在尝试创建一个密码文本字段,我希望用户输入至少 8 个字符。当设置了最大长度时,我真的很喜欢文本字段上可见的最大长度字符计数器的工作方式,我想知道是否有任何等效的最小长度。谢谢! 最佳答案 你必须
我已经使用远程数据源实现了 typeahead.js 和 bloodhound,它基本上按预期工作。但是,我已将 typeahead 上的 minLength 设置为 2,虽然我可以看到 ajax 请
为什么 minlenght 只能在 Chrome 中使用? Example on JSFIDDLE 最佳答案 根据this没有浏览器支持该功能。 尽管如此,我使用的是 Chrome 40
我有一个名为 Config 的常量,其中包含 task.minlength = 3 属性。 我将常量附加到 $rootScope 中: .run(function($rootScope, Config
我尝试仅在验证成功时提交表单。验证适用于 required 但不适用于 ng-minlength 表单输入无效,但表单仍在提交中。 Mobile
我在 http://jqueryui.com/demos/autocomplete/ 使用了“自定义数据和显示”示例并对其进行了一些简化,这样我就可以拥有值和 ID,如下所示: $(function(
如何在不影响其他元素的情况下为单个元素设置 minLength 编辑: 插件代码如下 (function ( $ ) { $.widget( "ui.combobox", {
我在手机输入中使用 $error.minlength,但是当我想验证此输入以启用按钮以向服务器发送表单时,$error.minlenght 值为空。在我输入内容之前它什么也没有显示,但是在我输入任何内
我正在尝试使用 ng-minlength 确保输入的最小长度,但它似乎在一种极端情况下失败了。它在添加或删除字符时正确验证,但如果删除整个输入,它将错误地返回到有效状态。这是一个已知缺陷,还是我错误地
我正在使用 jQuery 验证插件。 我想动态选择最小长度。 目前我正在尝试类似的方法,但它不起作用。 minlength:function () { if(val == 1 ) {
我有一个输入字段,用户可以通过它提交一些数据。 但是,如果数据字符长度小于10,那么他们将无法提交。 这意味着,如果用户尝试提交少于 10 个字符的数据,那么 google chrome 会显示此错
我需要将这两种模式组合在一起或在我的 中同时使用它们字段 我有模式匹配来检查有效时间 - 这很好用并且该字段是“不需要的”所以我们将接受一个空白字段 pattern="([01]?[0-9]|2[
我对 Node 还很陌生,所以可能是我没有正确使用 JSON Schema,如果我错了,请纠正我。 我一直在使用名为 jsonschema 的 npm 模块。 为了使用验证,我使用它如下: var V
似乎 minlength 的属性字段不起作用。 HTML5 中是否还有其他属性可以帮助我设置字段值的最小长度? 最佳答案 您可以使用 pattern attribute . required att
我是一名优秀的程序员,十分优秀!