在asp.net MVC中获取dropdownlist的选定值 [英] Get selected value of dropdownlist in asp.net MVC

查看:471
本文介绍了在asp.net MVC中获取dropdownlist的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取下拉列表的选择值,这里是我的代码正常工作。

how can i get select value of dropdownlist and here is my code which is working fine.

    var list = new[] {   
    new Person { Id = 1, Name = "Name1" }, 
    new Person { Id = 2, Name = "Name2" }, 
    new Person { Id = 3, Name = "Name3" } 
};

var selectList = new SelectList(list, "Id", "Name", 2);
ViewData["People"] = selectList;

<%= Html.DropDownListFor(model => model.Peoples, ViewData["People"] as IEnumerable<SelectListItem>)%>


推荐答案

model.Peoples 属性,您在 DropDownListFor 中使用的属性必须为整数。它将被绑定到您提交此表单的控制器操作中的选定值:

The model.Peoples property which you are using in the DropDownListFor must be of type integer. It will be bound to the selected value in the controller action to which you are submitting this form:

[HttpPost]
public ActionResult Index(YourModelType model)
{
    int selectedPersonId = model.Peoples;
    // TODO: do something with the id
    return View();
}

备注:人民币更符合语法正确的名称属性将为 PersonId

这篇关于在asp.net MVC中获取dropdownlist的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆