asp.net mvc2从下拉列表中获取id和值

发布于 2024-11-05 04:04:28 字数 407 浏览 0 评论 0原文

我见过仅获取 id 或仅获取值的示例,但我想从表单帖子中获取两者,这怎么可能?

当前代码:

<div class="editor-field">
 <%= Html.DropDownList("MySelectedItem", new SelectList(Model.Items, "Value", 
     "Key",Model.Items))%><small class="marker">*</small>
</div>

如果用户从下拉列表中选择第 3 项,我想进入控制器内部:

MySelectedItem.Value = 3
MySelectedItem.Key= "something"

I have seen examples of getting the id only or value only but I want to get both from the form post, how is this possible?

current code:

<div class="editor-field">
 <%= Html.DropDownList("MySelectedItem", new SelectList(Model.Items, "Value", 
     "Key",Model.Items))%><small class="marker">*</small>
</div>

If a user selects item 3 from the drop down I want to get inside the controller:

MySelectedItem.Value = 3
MySelectedItem.Key= "something"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

纸短情长 2024-11-12 04:04:28

听起来你应该更应该致力于代码设计。密钥应该是您识别该值所需的全部内容。为什么你需要知道价值?

你谈到了你的价值观在桌子之间的冲突。这也不应该是一个问题。听起来您可能也想重新设计数据库设计。键和唯一约束是数据库中唯一应该唯一的字段。

It sounds like you should rather work on your code design. The key should be all you needed to identify the value. Why would you need to know the value?

You speak about your value clashing between tables. This should not be an issue either. It sounds like you might want to rework your database design as well. Keys and unique constraints are the only fields in your database that should be unique.

独﹏钓一江月 2024-11-12 04:04:28

不如使用 JQuery 在更改事件中弹出隐藏字段中的一个值,简单又简单,问题就解决了。

How about just pop of one the values in hidden field on change event using JQuery, easy and simple and you issue solved.

凡间太子 2024-11-12 04:04:28

(除非我误解了..)

在浏览器中,您将看到类似的内容

<select name="MySelectedItem">
    <option value="Model.Item.Value1">Model.Item.Key1</option>
</select>

当发布表单时,您将拥有 Request.Form["MySelectedItem"] ,其值为“Model.Item.Value1”,这意味着您有一个几个选择

  1. 当下拉列表更改时填充客户端的隐藏字段或
  2. 在控制器中重新加载您的集合并在以下位置找到该项目:

    Item.Value == Request.Form["MySelectedItem"]

(unless I'm misunderstanding..)

In the browser you'll have something like

<select name="MySelectedItem">
    <option value="Model.Item.Value1">Model.Item.Key1</option>
</select>

When the form is posted you will have Request.Form["MySelectedItem"] with a value of "Model.Item.Value1" which means you have a couple of choices

  1. Populate a hidden field on the client side when the dropdown changes or
  2. Reload your collection in the controller and find the item where:

    Item.Value == Request.Form["MySelectedItem"]

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