如何获取 MVC 视图本身中下拉列表的选定值

发布于 2024-08-16 06:31:21 字数 464 浏览 9 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

万水千山粽是情ミ 2024-08-23 06:31:21

阅读您的问题后,听起来您希望下拉列表为同一页面的下部提供一个值。

首先,您需要将 DropDownList 放置在表单构造中,如下所示:

<% using (Html.BeginForm("ProcessValue", "ThisPage")) { %>
    <%= Html.DropDownList("DropID", Range(0, 10).Select(a=>new SelectListItem { Text = x, Value = x }) %>
    <input type=submit value="Submit" />
<% } %>

您需要提前设置一些内容:

  1. 您必须有一个提交按钮或类似的构造,以便检索值DropID 变量的。
  2. 您需要设置一个控制器方法来处理该值,然后重定向回原始页面,并在页面的 ViewData 中选择值。
  3. 最后,您需要设置视图,以便仅当 DropID 变量中有有效值时才显示后处理部分。

它并不像仅仅将 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:

<% using (Html.BeginForm("ProcessValue", "ThisPage")) { %>
    <%= Html.DropDownList("DropID", Range(0, 10).Select(a=>new SelectListItem { Text = x, Value = x }) %>
    <input type=submit value="Submit" />
<% } %>

You need to set up a few things ahead of time:

  1. You have to have a submit button, or a similar construct, in order to retrieve the value of the DropID variable.
  2. You need to set up an controller method that will handle the processing of the value, then redirect back to the original page with the selected value in the page's ViewData.
  3. Finally, you need to set up the view so that the post-processing section will only display if you have a valid value in the DropID variable.

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).

浅唱々樱花落 2024-08-23 06:31:21

要获取选定的值,您可以使用 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.

话少心凉 2024-08-23 06:31:21

所选值将位于 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.

蝶舞 2024-08-23 06:31:21

还有一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文