Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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获取选定值_C#_Asp.net Mvc_Asp.net Mvc 5 - Fatal编程技术网

C# 如何从dropdownlistfor获取选定值

C# 如何从dropdownlistfor获取选定值,c#,asp.net-mvc,asp.net-mvc-5,C#,Asp.net Mvc,Asp.net Mvc 5,我正在从模型绑定dropdownlist,但无法从dropdownlist获取所选值,它显示错误: 提交表单时,值“1”无效 模型代码 控制器代码 填充下拉列表 [HttpGet] 公众行动结果登记册() { var model=新寄存器(); List stState=新列表(); List SelectListItemState=新建列表(); 添加(新的SelectListItem(){Value=string.Empty,Text=“Select State”}); 添加(新的Selec

我正在从模型绑定dropdownlist,但无法从dropdownlist获取所选值,它显示错误:

提交表单时,值“1”无效

模型代码 控制器代码 填充下拉列表
[HttpGet]
公众行动结果登记册()
{
var model=新寄存器();
List stState=新列表();
List SelectListItemState=新建列表();
添加(新的SelectListItem(){Value=string.Empty,Text=“Select State”});
添加(新的SelectListItem(){Value=“1”,Text=“Mentor”});
添加(新的SelectListItem(){Value=“1”,Text=“Employee”});
添加(新的SelectListItem(){Value=“1”,Text=“Finance”});
model.usertype=selectListItemState.ToList();
返回视图(模型);
}

您需要在模型中为SelectedValue添加一个属性,该属性很可能是
int

当前,您正在使用不正确的
SelectList
绑定
DropDownListFor
DropDownListFor
将发布所选的单个值,通常为整数:

    public List<SelectListItem> usertype { get; set; }

    [Display(Name="User Type")]
    [Required(ErrorMessage = "Select user type")]
    public int UserType { get; set; }

每个SelectListItem的值为1?那是不对的…@Kjata:是的,那只是为了演示。我知道,我是故意这么做的。重点是你的代码示例应该尽可能准确,最好不要让读者感到困惑。请更新。好的,让我把它分解一下。我想添加强类型的dropdownlist,带有验证,还想从dropdownlist中获取所选值。我可以做前2件事,但无法获取所选值,在发布页面时,页面上显示“值'1'无效”,请编辑您的问题以使其更清晰,不要试图在评论中澄清。我尝试过这件事,但使用下拉列表无法验证。您在其上添加了数据批注吗?好的,让我来分析一下。我想添加强类型的dropdownlist,带有验证,还想从dropdownlist中获取所选值。我可以做前2件事,但无法获取所选值,而在发布页面时,它会说,“值'1'无效”,因为您正在发布
列表
作为所选值,这不正确。好的,但是如何获取所选值。根据您的代码,您正在将UserType与dropdownlistfor绑定,但它将如何获得所选值,我不理解。。
@Html.DropDownListFor(m => m.usertype, Model.usertype, new {  @class="form-control input-lg"})
//controller
[HttpPost]
public ActionResult Register(Register register,FormCollection form)
{
}
[HttpGet]
public ActionResult Register()
{
    var model=new Register();
    List<string> stState = new List<string>();
    List<SelectListItem> selectListItemStates = new List<SelectListItem>();
    selectListItemStates.Add(new SelectListItem() { Value = string.Empty, Text = "Select State" });
    selectListItemStates.Add(new SelectListItem() { Value = "1", Text = "Mentor" });
    selectListItemStates.Add(new SelectListItem() { Value = "1", Text = "Employee" });
    selectListItemStates.Add(new SelectListItem() { Value ="1", Text = "Finance" });


    model.usertype = selectListItemStates.ToList();
    return View(model);
}
    public List<SelectListItem> usertype { get; set; }

    [Display(Name="User Type")]
    [Required(ErrorMessage = "Select user type")]
    public int UserType { get; set; }
@Html.DropDownListFor(m=>m.UserType,Model.usertype, new {  @class="form-control input-lg"}