本文介绍了MVC3可选机型物业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想是可选的,但是, ModelState.IsValid 检查正试图验证的属性一个DropDownList。有没有办法告诉它的属性是可选的?

I have a dropdownlist which I would like to be optional, however, the ModelState.IsValid check is trying to validate the property. Is there a way to tell it the property is optional?

下面是我的code:

<h2>New Version</h2>

@using (Html.BeginForm()) {
    <div>Version Number: @Html.TextBoxFor(m => m.Number)</div>
    <div>Enabled: @Html.CheckBoxFor(m => m.IsActive)</div>

    <div></div>

    <div>
        Template (optional): 
        @Html.DropDownListFor(m => m.TemplateVersionId, new SelectList(Model.CurrentVersions, "VersionId", "Number"),
            "-- Select Template --")
    </div>

    <input type="submit" value="Save" />
    @Html.ActionLink("Cancel", "Index");
}

和我的视图模型:

public class AddVersionViewModel
{
    public double Number { get; set; }
    public bool IsActive { get; set; }

    public int TemplateVersionId { get; set; }
    public ICollection<Version> CurrentVersions { get; set; }
}

我要的 TemplateVersionId 是可选的。

推荐答案

将其更改为 INT?,使其可为空。

Change it to an int? to make it nullable.

这篇关于MVC3可选机型物业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 17:53