如何使DropDownListFor绑定到Nullable <int>属性接受一个空值?

Jea*_*amp 9 c# validation nullable asp.net-mvc-3 drop-down-menu

我在ASP.NET MVC cshtml页面中有以下DropDownList:

@Html.DropDownListFor(model => model.GroupId, (IEnumerable<SelectListItem>)ViewBag.PossibleGroups, "")
Run Code Online (Sandbox Code Playgroud)

该属性定义为 public virtual Nullable<int> GroupId

虽然GroupIdNullableselect菜单将不会接受一个空的选项.如果我尝试在不选择组的情况下保存模型,则会收到以下错误消息:

该字段GroupId必须是数字.

选择菜单的呈现方式如下:

<select data-val="true" data-val-number="The field GroupId must be a number." id="GroupId" name="GroupId" class="input-validation-error" data-hasqtip="0" aria-describedby="qtip-0">
    <option value=""></option>
    <option value="1">Test Group</option>
</select>
Run Code Online (Sandbox Code Playgroud)

不,GroupId没有用[Required]属性装饰.

我怎样才能使页面接受空值GroupId

PS GroupIdtype(Nullable<int>)是代码生成的.我使用数据库优先方案.我不知道虽然<Nullable>int和之间有什么区别int

更新:

即使为空选择项的值为零也不会通过非阻碍(JavaScript)验证.但是,-1在客户端和服务器端验证时都要传递一个值.所以现在,我正在使用这个值,在控制器中,我设置GroupIdnull等于-1.

我无法相信这样一个简单的问题没有更简单的解决方案.这是ASP.NET MVC 3中的错误吗?

小智 0

为模型绑定创建类

 public class BindDropDown
{
    public Nullable<int> ID{ get; set; }
    [Required(ErrorMessage = "Enter name")]
    public string Name{ get; set; } 
}
Run Code Online (Sandbox Code Playgroud)

创建控制器

    namespace DropDown.Controllers
  {
public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";

        ViewBag.InterviewList = NumberList;

        return View(new BindDropDown());
    }
    [HttpPost]
    public ActionResult Index(BindDropDown d)
    {
        if (ModelState.IsValid)
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
        }
        else
        {
            ViewBag.Message = "error";
        }
        ViewBag.NumberList = NumberList;

        return View(new BindDropDown());
    }

    public ActionResult About()
    {
        return View();
    }

    public static IEnumerable<SelectListItem> NumberList
    {
        get
        {
            IEnumerable<NumberClass> interviewAppeals = Enum.GetValues(typeof(NumberClass))
                                                  .Cast<NumberClass>();
            return from item in interviewAppeals
                   select new SelectListItem
                   {
                       Text = item.ToString(),
                       Value = ((int)item).ToString()
                   };

        }
    }

}
public enum NumberClass
{
    One = 1,
    Two = 2,
    Three = 3
}
 }
Run Code Online (Sandbox Code Playgroud)

索引.cshtml

@using DropDown.Models
@model BindDropDown
@{
ViewBag.Title = "Home Page";
 }

<h2>@ViewBag.Message</h2>
<p>

</p>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id =   "frmOnline" }))
{

@Html.ValidationSummary(false)   


<div class="w100">
    <div class="fleft">
        <label>ID :</label>
    </div>
    <div class="fleft">
        @Html.DropDownListFor(model => model.ID, (IEnumerable<SelectListItem>)ViewBag.NumberList, "")

    </div>
</div> 
<div class="w100">
    <div class="fleft">
        <label>Name :</label>
    </div>
    <div class="fleft">
        @Html.TextBoxFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)

    </div>
</div> 
<div class="w100 clear">
    <label>&nbsp;</label>
    <div class="fleft">
        <input type="submit" value="Save" class="button green" />
        @Html.ActionLink("Back", "GetAllAction", null, new { @class = "button gray" })

    </div>
</div> 
}
Run Code Online (Sandbox Code Playgroud)