Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将DropDownListFor绑定到可为空的<;int>;属性是否接受空值?_C#_Asp.net Mvc 3_Validation_Drop Down Menu_Nullable - Fatal编程技术网

C# 如何将DropDownListFor绑定到可为空的<;int>;属性是否接受空值?

C# 如何将DropDownListFor绑定到可为空的<;int>;属性是否接受空值?,c#,asp.net-mvc-3,validation,drop-down-menu,nullable,C#,Asp.net Mvc 3,Validation,Drop Down Menu,Nullable,我在ASP.NET MVC cshtml页面中有以下下拉列表: @Html.DropDownListFor(model => model.GroupId, (IEnumerable<SelectListItem>)ViewBag.PossibleGroups, "") 不,GroupId没有使用[Required]属性修饰 如何使页面接受GroupId的空值 另外,GroupId类型(Nullable)是由代码生成的。我使用数据库优先方案。我不知道int和int之间有什么区别

我在ASP.NET MVC cshtml页面中有以下下拉列表:

@Html.DropDownListFor(model => model.GroupId, (IEnumerable<SelectListItem>)ViewBag.PossibleGroups, "")
不,
GroupId
没有使用
[Required]
属性修饰

如何使页面接受
GroupId
的空值

另外,
GroupId
类型(
Nullable
)是由代码生成的。我使用数据库优先方案。我不知道
int
int
之间有什么区别

更新

即使空选择项的值为零,也无法通过非强制(JavaScript)验证。但是,输入值
-1
会通过客户端和服务器端验证。现在,我使用这个值,在控制器中,我将
GroupId
设置为
null
,如果它等于
-1


我不敢相信,对于这样一个简单的问题,没有更简单的解决办法。这是ASP.NET MVC 3中的错误吗?

为模型绑定创建类

 public class BindDropDown
{
    public Nullable<int> ID{ get; set; }
    [Required(ErrorMessage = "Enter name")]
    public string Name{ get; set; } 
}
公共类绑定下拉列表
{
公共可空ID{get;set;}
[必需(ErrorMessage=“输入名称”)]
公共字符串名称{get;set;}
}
创建控制器

    List<Products> lstProducts = new List<Products>()
    {
    new Products() {pid=0, sValue=""},
    new Products() {pid=1, sValue="One"},
    new Products() {pid=2, sValue="Two"}
    };

ViewBag.products = new SelectList(lstProducts, "pid", "sValue");
名称空间下拉列表.控制器
{
公共类HomeController:控制器
{
公共行动结果索引()
{
ViewBag.Message=“欢迎使用ASP.NET MVC!”;
ViewBag.InterviewList=NumberList;
返回视图(新建BindDropDown());
}
[HttpPost]
公共行动结果索引(BindD下拉列表)
{
if(ModelState.IsValid)
{
ViewBag.Message=“欢迎使用ASP.NET MVC!”;
}
其他的
{
ViewBag.Message=“错误”;
}
ViewBag.NumberList=NumberList;
返回视图(新建BindDropDown());
}
关于()的公共行动结果
{
返回视图();
}
公共静态IEnumberList
{
得到
{
IEnumerable InterviewInvestments=Enum.GetValues(typeof(NumberClass))
.Cast();
从访谈中的项目返回
选择新的SelectListItem
{
Text=item.ToString(),
Value=((int)项).ToString()
};
}
}
}
公共枚举数类
{
1=1,
二等于二,
三=3
}
}
Index.cshtml

@使用下拉菜单。模型
@模型绑定下拉列表
@{
ViewBag.Title=“主页”;
}
@查看包。留言

@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{id=“frmOnline”})) { @Html.ValidationSummary(false) 身份证件: @Html.DropDownListFor(model=>model.ID,(IEnumerable)ViewBag.NumberList,“”) 姓名: @Html.TextBoxFor(model=>model.Name) @Html.ValidationMessageFor(model=>model.Name) @ActionLink(“Back”,“GetAllAction”,null,new{@class=“button gray”}) }
尝试在下拉列表中显示空白值 模型

控制器

    List<Products> lstProducts = new List<Products>()
    {
    new Products() {pid=0, sValue=""},
    new Products() {pid=1, sValue="One"},
    new Products() {pid=2, sValue="Two"}
    };

ViewBag.products = new SelectList(lstProducts, "pid", "sValue");
List lstProducts=新列表()
{
新产品(){pid=0,sValue=”“},
新产品(){pid=1,sValue=“One”},
新产品(){pid=2,sValue=“Two”}
};
ViewBag.products=新的选择列表(lstProducts,“pid”,“sValue”);
看法

@Html.DropDownList(“产品”,@ViewBag.products作为列表)

GroupId是否用其他属性修饰?
@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> 
}
public class Products {
public int pid {get;set;}
public String sValue {get; set;}
}
    List<Products> lstProducts = new List<Products>()
    {
    new Products() {pid=0, sValue=""},
    new Products() {pid=1, sValue="One"},
    new Products() {pid=2, sValue="Two"}
    };

ViewBag.products = new SelectList(lstProducts, "pid", "sValue");
@Html.DropDownList("products",@ViewBag.products as List<SelectListItems>)