如何在Mvc中获取选定值,下拉列表的选定索引 [英] How Do I Get The Selected Value, Selected Index Of A Drop Down List In Mvc

查看:141
本文介绍了如何在Mvc中获取选定值,下拉列表的选定索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello House,



i来自asp.net拖放背景,我希望能够获得下拉列表的选定值,选定索引等控制mvc5中的控制器代码。这是我的查看代码:



Hello House,

i came from asp.net drag and drop background, i want to be able to get the selected value, selected index etc of a drop downlist control in the controller code in mvc5. here is my view code:

@Html.DropDownList("Role",new List<SelectListItem>
                        {
                             new SelectListItem{Text="Select Role", Value="0",Selected =true } ,
                            new SelectListItem{Text="Student", Value="1" } ,
                            new SelectListItem{Text="Lecturer", Value="2"  }
                           
                        }, "") 





任何帮助将不胜感激。



Any assistance will be appreciated.

推荐答案

首先你应该定义视图模型,然后将你的控件绑定到模型上



first you should define view model and than bind your control to model

@model ViewModel.someViewModel
 @Html.DropDownListFor(model => model.ID, new SelectList(ViewBag.Roll, "ID", "Name"))

then in your controller action method should receive  data

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Update(viewModel model)
{
//do some thing
}


这篇关于如何在Mvc中获取选定值,下拉列表的选定索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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