简体   繁体   English

MVC 4下拉验证

[英]MVC 4 Dropdown validation

OK, this is driving me nuts. 好,这真让我发疯。 I've got an MVC 4 website that is validating textboxes on the client, but not the dropdowns. 我有一个MVC 4网站,该网站正在验证客户端上的文本框,而不是下拉列表。

My (cut-down) model looks like this: 我的(缩减)模型如下所示:

[Required]
public int? FabricOptionSelected { get; set; }
public List<SelectListItem> FabricOptions { get; private set; }

My (cut-down) controller looks like this: 我的(缩减)控制器如下所示:

model.FabricOptions.AddRange(product.Options
.Where(a=>a.ProductOptionCategory == Domain.Products.ProductOptionCategory.Fabric)
.ToSelectList( m => m.Name, m => m.Id.ToString(), "-- Select --"));

My (cut-down) view looks like this: 我的(缩小的)视图如下所示:

@if (Model.FabricOptions.Count > 1)
    {
        <div class="row form-group">
            <div class="col-xs-2 control-label">Fabric</div>
            <div class="col-xs-4">@Html.DropDownListFor(m => m.FabricOptionSelected, Model.FabricOptions, new { @class = "form-control" })</div>
            <div class="col-xs-6">@Html.ValidationMessageFor(m => m.FabricOptionSelected)</div>
        </div>
    }

I have "~/Scripts/jquery.validate.js", and "~/Scripts/jquery.validate.unobtrusive.js" in my bundle config and the web.config has : 我的捆绑包配置中包含“〜/ Scripts / jquery.validate.js”和“〜/ Scripts / jquery.validate.unobtrusive.js”,而web.config具有:

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

in the appsettings section. 在“设置”部分中。

Chrome console is not showing any errors and there is a text box with the Required attribute further down the page that is being fired. Chrome控制台未显示任何错误,并且在被触发的页面下方还有一个带有Required属性的文本框。

.ToSelectList is an extension method: .ToSelectList是扩展方法:

public static List<SelectListItem> ToSelectList<T>
        (this IEnumerable<T> enumerable, 
        Func<T, string> text, 
        Func<T, string> value, 
        string defaultOption)
    {
        var items = enumerable.Select(f => new SelectListItem() { Text = text(f), Value = value(f) }).ToList();
        items.Insert(0, new SelectListItem() { Text = defaultOption, Value = "-1" });
        return items;
    } 

Can't figure out why the client-side validation in the view isn't firing for the drop-downs. 无法弄清楚为什么视图中的客户端验证没有触发下拉菜单。 I figure I'm missing something obvious but could really do with a hand on this one. 我认为我遗漏了一些明显的东西,但确实可以做到这一点。

Make sure .ToSelectList (is this an extension you added?) sets the default -- Select -- value to null . 确保.ToSelectList (这是你添加一个扩展?)设置默认-- Select --null Setting it to a nullable type is a good start but it needs to be null for Required to work. 将其设置为可为null的类型是一个好的开始,但是必须为null才能使Required正常工作。

Your extension method is inserting a "default" option which has a value="-1" which is valid for an int therefore there will be no error. 您的扩展方法正在插入一个“ default”选项,该选项的value="-1"int有效,因此不会出现错误。 You can achieve want you want using methods already available in MVC 您可以使用MVC中已经可用的方法来实现所需的目标

Model 模型

[Required]
public int? FabricOptionSelected { get; set; }
public SelectList FabricOptions { get; set; }

Controller 控制者

var option = product.Options.Where(a=>a.ProductOptionCategory == Domain.Products.ProductOptionCategory.Fabric);
model.FabricOptions = new SelectList(options, "ID", "Name");

View 视图

@Html.DropDownListFor(m => m.FabricOptionSelected, Model.FabricOptions, "-- Select --", new { @class = "form-control" })

The 3rd parameter renders the first option of the <select> as <option value>--Select--</option> ie without a value so if that is selected, int? FabricOptionSelected 第三个参数将<select>的第一个选项呈现为<option value>--Select--</option>即没有值,因此如果选择了该值,则为int? FabricOptionSelected int? FabricOptionSelected will be null and therefore fail validation because of the [Required] attribute int? FabricOptionSelected将为null ,因此由于[Required]属性而导致验证失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM