gpt4 book ai didi

asp.net-mvc - MVC 4 - 如何验证下拉列表?

转载 作者:行者123 更新时间:2023-12-02 17:47:11 25 4
gpt4 key购买 nike

我有一个 .Net MVC4 模型/ View ,其中包含许多 [必需] 字段,其中一个是通过下拉列表“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>

最佳答案

您需要使用 DropDownListForYourSelectList 如下;

<div class="editor-field">

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

</div>

关于asp.net-mvc - MVC 4 - 如何验证下拉列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13359042/

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