如何在 MVC 的控制器中获取 DropDownList SelectedValue
问题描述
I have dropdownlist, which I have filled from database. Now I need to get the selected value in Controller do some manipulation. But not getting the idea. Code which I have tried.
Model
Controller
View
1st Approach (via Request or FormCollection):
You can read it from Request
using Request.Form
, your dropdown name is ddlVendor
so pass ddlVendor
key in the formCollection to get its value that is posted by form:
or Use FormCollection
:
2nd Approach (Via Model):
If you want with Model binding then add a property in Model:
and in View:
and in Action:
UPDATE:
If you want to post the text of selected item as well, you have to add a hidden field and on drop down selection change set selected item text in the hidden field:
use jquery to set hidden field:
这篇关于如何在 MVC 的控制器中获取 DropDownList SelectedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!