如何获取 MVC 视图本身中下拉列表的选定值
我在 MVC 视图中有一个下拉列表,它是这样的:
Html.DropDownList(id, Range(0,10)
.Select(x => new SelectListItem {Text = x, Value = x}))
在视图本身中,我需要该下拉列表的选定值。我知道我可以在 JavaScript 中访问它,但我正在寻找一种方法来从下拉属性或类似的东西中获取它。
我怎样才能访问它?我试图从智能感知中找出一些东西,但没有出现任何相关内容,非常感谢帮助。
编辑:我想要下拉声明后几行后的值,我知道我可以从JavaScript并通过发布表单来访问它,是否无法在视图本身上访问它?
编辑2:如果无法在视图中访问它,请解释原因,我更有兴趣了解它。
谢谢。
I have a drop down in a MVC View, which is some thing like this:
Html.DropDownList(id, Range(0,10)
.Select(x => new SelectListItem {Text = x, Value = x}))
In the view itself, I need the selected value of this drop down. I know that I can access that in a JavaScript, but I am looking for a way to get it in the view itself from the drop down properties or something like that.
How can I access it? I tried to figure out some thing from intellisense but nothing relavant showed up, help is much appreciated.
Edit: I want the value after a few lines after the declaration of the drop down, I know that I can access it from JavaScript and by posting the form, Is there noway to access it on the view itself ?
Edit2: If its not possible to access it in view, please explain the reason, I am more interested in knowing it.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
阅读您的问题后,听起来您希望下拉列表为同一页面的下部提供一个值。
首先,您需要将 DropDownList 放置在表单构造中,如下所示:
您需要提前设置一些内容:
它并不像仅仅将 DropDownList 放置在视图中然后在页面中使用该值那么简单。您必须让控制器参与管理数据传输,并且必须设置单个视图来处理多个状态(即在选择值之前和选择发生之后)。
After reading at your question, it sounds like you want to have the drop down list supply a value for a lower section of the same page.
First and foremost, you will need to place the DropDownList within a form construct, as in:
You need to set up a few things ahead of time:
It's not as simple as just placing a DropDownList in a view and then using that value later on in the page. You have to get the controller involved to manage the data transport and you have to set up the single view to handle multiple states (ie. before the value is selected and after the selection takes place).
要获取选定的值,您可以使用 javascript 或控制器操作来提交包含选择框的表单。
To get the selected value you could use either javascript or a controller action to which the form containing the select box is submitted.
所选值将位于 FormCollection 或 QueryString 集合中,其名称为从该视图接收表单提交的控制器操作中的 DropDown ID。您可以使用经典的 POST 或 GET 或通过 AJAX 提交值,但必须连接处理该输入的服务器端操作。
The selected value will be in the FormCollection or QueryString collection with the name of the DropDown's ID in the controller action that receives the form submission from this view. You can either submit the values with a classic POST or GET or via AJAX, but you have to wire up a server-side action that processes that input.
还有一个 SelectList 类,它允许您定义将选择哪个项目..并且知道 - 您将知道所选值..
另外..如果没有明确选择任何值,下拉列表往往会选择列表中的第一项。
There is a SelectList class as well wich allows you to define wich item will be selected.. and knowing that - you will know selected value..
Also.. if no value is selected explicitly, dropdowns tend to select the first item in the list.