2

我有一个 .Net MVC4 模型/视图,其中包含许多 [必需] 字段,其中一个是通过下拉列表“Content_CreatedBy”[下面的第一个代码块]选择的。

客户端验证在除 DDL 之外的所有字段上触发 [尽管服务器端验证不允许 DDL 中的任何条目]。我尝试验证 DDL 文本及其数值,但在客户端没有触发。

谁能看到我做错了什么?

谢谢
模型

[Required]
[Display(Name = "Author")]
[ForeignKey("ContentContrib")]
[Range(1, 99, ErrorMessage = "Author field is required.")]
public virtual int Content_CreatedBy { get; set; }

[Required]
[Display(Name = "Date")]
public virtual DateTime Content_CreatedDate { get; set; }

[Required]
[DataType(DataType.MultilineText)]
[Display(Name = "Source / Notes ")]
[StringLength(10, MinimumLength = 3)]
public virtual string Content_Sources { get; set; }

[Required]
[Display(Name = "Keywords")]
[StringLength(50, MinimumLength = 3)]
public virtual string Content_KeyWords { get; set; }

看法

<div class="editor-label">
@Html.LabelFor(model => model.Content_CreatedBy, new { @class="whitelabel"})
</div>

<div class="editor-field">

@Html.DropDownList("Content_CreatedBy", String.Empty)
@Html.EditorFor(model => model.Content_CreatedBy)
@Html.ValidationMessageFor(model => model.Content_CreatedBy)
</div>
4

3 回答 3

4

DropDownListFor还需要YourSelectList按如下方式使用;

<div class="editor-field">

   @Html.DropDownListFor(model => model.Content_CreatedBy, 
                                  YourSelectList,
                                 "[ -- Please Select --]") 
   @Html.ValidationMessageFor(model => model.Content_CreatedBy)

</div>
于 2012-11-13T10:37:02.883 回答
2

使Content_CreatedBy可空。

[Required]
[Display(Name = "Author")]
[ForeignKey("ContentContrib")]
[Range(1, 99, ErrorMessage = "Author field is required.")]
public virtual int? Content_CreatedBy { get; set; }
于 2012-11-13T10:57:49.953 回答
0

由于您的属性是 integrt 类型,如果找不到匹配的活页夹,它总是初始化为 0,因此它永远不会给你客户 vaidation,

解决方法,将类型更改为字符串,也使用类似的东西。

[Required]
[Display(Name = "Author")]
[ForeignKey("ContentContrib")]
[Range(1, 99, ErrorMessage = "Author field is required.")]
public virtual string Content_CreatedBy { get; set; }



Html.DropDownListFor(model => model.Content_CreatedBy, 
                              IEnumerable<object>,
                             " -- Please Select --") 
于 2012-11-13T18:21:07.667 回答