- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有两个具有一对多关系的模型类。
public class CycleType
{
[Required(ErrorMessage = "Cycle is required.")]
public int CycleTypeID { get; set; }
[Required(ErrorMessage = "Cycle Type is required.")]
[StringLength(20, ErrorMessage = "Cycle Type may not be longer than 20 characters")]
public string Type { get; set; }
public List<CycleModel> CycleModels { get; set; }
}
public class CycleModel
{
public int CycleModelID { get; set; }
[DisplayName("Cycle Type")]
[Required(ErrorMessage = "Cycle is required.")]
public int CycleTypeID { get; set; }
[Required(ErrorMessage = "Model is required.")]
[StringLength(20, ErrorMessage = "Model may not be longer than 20 characters")]
public string Model { get; set; }
public virtual CycleType CycleType { get; set; }
}
Razor 文件。
<div class="editor-label">
@Html.LabelFor(model => model.CycleTypeID)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.CycleTypeID,
(SelectList)ViewBag.CycleType,
"Select Cycle Type",
new { id = "ddlCycleType" })
@Html.ValidationMessageFor(model => model.CycleTypeID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Model)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Model)
@Html.ValidationMessageFor(model => model.Model)
</div>
1) 我的问题是,当我选择选择循环类型时,Validaion 函数没有触发,它只返回错误
The ViewData item that has the key 'CycleTypeID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
2) 第二个问题是,当我选择其中一种循环类型并将值放入超过 20 个字符的模型中,以便验证器可以按我的预期进行检查。但是我再次收到相同的错误消息。
The ViewData item that has the key 'CycleTypeID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.
我们将不胜感激。
最佳答案
嗯,这是 Asp.net MVC 中的一个错误,因此验证不适用于 DropDownListFor 和 TextAreaFor 扩展方法。
您可以在http://aspnet.codeplex.com/workitem/8576查看详情
因此,您将需要使用 HTML.DropDownList 而不是 DropDownListFor,然后您的验证将按预期工作。
更新
从你的 Controller ,传递这个
ViewBag.CycleTypeId = new SelectList(db.CycleTypes, "CycleTypeId", "Type"); // db is ur context instance
在 View 中使用这个
@Html.DropDownList("CycleTypeId", String.Empty)
更多更新
这个问题还有一个解决方法。
只需使用您的原始代码 DropDownListFor
。然后像下面这样使用类属性
@Html.DropDownListFor(model => model.CycleTypeID,
(SelectList)ViewBag.CycleType,
"Select Cycle Type",
new { id = "ddlCycleType", @class = "required" })
这将使验证生效,但它会显示默认消息The field is required。但 else 将按您预期的那样工作。
我想这是一个更好的解决方案,也很适合您的要求。
关于c# - @Html.ValidationMessageFor 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8830648/
我有一个部分 View : @Html.EditorFor(model => model.BM) @Html.ValidationMessageFor(model => model.BM) 在主视
我如何检查模型属性是否存在验证错误 - 我正在使用 validationmessagefor 方法来显示错误消息。我怎样才能一次只显示一条错误消息?我 这是我拥有的模型属性 m.Communicati
我正在尝试对ASP.NET MVC3中的@Html.ValidationMessageFor()使用其他消息。效果很好,但似乎总是显示该消息,例如如果我这样做: @Html.ValidationMes
几天以来,我已经开始努力反对它,尝试了所有的想法(无论是否愚蠢),但似乎我无法让它发挥作用。 我想要存档的是:我只想显示一个带有 jquery 工具提示的图标,而不是原始的验证消息。此外,该解决方案应
你好, 我在我的页面上放置了以下代码: 然后在每个属性上我都有类似的东西: model.ModelViewAd.Title, "*", new { @class = "val" })%> 问题是,
我在页面顶部进行了一些验证。我只想在出现错误时才动态显示水平行。 @Html.ValidationMessageFor(model => model.tempHouseHoldSSNVerificat
我有两个具有一对多关系的模型类。 public class CycleType { [Required(ErrorMessage = "Cycle is required.")] pu
可能是个简单的问题,但我似乎找不到答案。 使用 MVC 2 我有一系列 Html.ValidationFor 控件。我想为文本分配一个 CSS 类,但似乎做不到。 Model.Chest, new
我的观点是: User Registration @Html.LabelFor(m => m.UsrName
为什么我的 @Html.ValidationMessageFo 不工作?当我运行该应用程序时,什么也没有发生,它允许输入所有内容。当我尝试在下面的编辑 View 中编辑项目时,它也会崩溃。我有以下内容
如果用户输入了无效的内容(比如需要数字的字符串),我试图让我的 View 在文本框旁边给我错误消息。相反,当用户按下提交时,我收到一个丑陋的错误页面,提示验证失败。 以下是我的部分观点: @model
我想在我的ASP.NET MVC应用程序中使用DataAnnotations。我已强类型化资源类,并想在我的 View 模型中定义: [DisplayName(CTRes.UserName)] str
当我的页面加载时,我的 ValidationSummary 和 ValidationMessageFor(model => model.TestNumbersString) 都显示验证错误,尽管几乎没
我有这门课: public class Product { [Required(ErrorMessage = "empty name")] public string Name { g
我想以 dd-mmm-yyyy 的形式强制执行日期。我使用以下帖子作为指南: http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-ne
出于验证目的,我使用了 validationMessage 和 ValidationMessageFor Helpers。两者都显示错误消息,那么验证消息和验证消息之间的主要区别是什么。我在各种网站上
我一直致力于为 MVC 创建自定义错误验证消息,以显示 jQueryUI 样式的错误消息。 到目前为止看起来很酷,但我需要在没有错误消息的情况下隐藏整个 div...我该怎么做? 这是我的 html
我使用的是标准 MVC 成员身份验证的变体。 鉴于我的模型有: [Required] [Display(Name = "User name")] public string U
我有一些文本字段的服务器端验证消息。字段“title”是必填字段服务器端,在类中具有“[Required]”数据属性,但这似乎只在回发或表单提交时进行检查。我将“标题”文本字段保存在 jquery .
我正在为我的 Web 应用程序使用 Bootstrap ,但我在将消息状态置于正确位置的验证消息方面遇到了问题。现在我使用了这个 link 中的示例, 使用此代码: Input with err
我是一名优秀的程序员,十分优秀!