asp.net mvc2从下拉列表中获取id和值
我见过仅获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你应该更应该致力于代码设计。密钥应该是您识别该值所需的全部内容。为什么你需要知道价值?
你谈到了你的价值观在桌子之间的冲突。这也不应该是一个问题。听起来您可能也想重新设计数据库设计。键和唯一约束是数据库中唯一应该唯一的字段。
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.
不如使用 JQuery 在更改事件中弹出隐藏字段中的一个值,简单又简单,问题就解决了。
How about just pop of one the values in hidden field on change event using JQuery, easy and simple and you issue solved.
(除非我误解了..)
在浏览器中,您将看到类似的内容
当发布表单时,您将拥有 Request.Form["MySelectedItem"] ,其值为“Model.Item.Value1”,这意味着您有一个几个选择
在控制器中重新加载您的集合并在以下位置找到该项目:
Item.Value == Request.Form["MySelectedItem"]
(unless I'm misunderstanding..)
In the browser you'll have something like
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
Reload your collection in the controller and find the item where:
Item.Value == Request.Form["MySelectedItem"]