从列表框中获取所选项目并将其作为参数传递给mvc [英] get selected item from listbox and pass it as parameters mvc

查看:48
本文介绍了从列表框中获取所选项目并将其作为参数传递给mvc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP ListBox,我已经从数据库中检索了项目.如何从列表框中获取所选项目并将其作为参数传递,以便通过按钮单击来调用操作?

I have an asp ListBox, I have retrieved the items from the database. How can I get the selected item from the Listbox and pass it as a parameter in order to call an action by a Button Click?

推荐答案

您可以做一些简单的事情如

You could do something as simple as

Public Sub Button_Click() Handles Button.Click
Dim Something as String = ListBox.SelectedValue 
'(Or SelectedText - If that is what you want)

SomeFunction(Something)



如果您给我一个代码摘录或更多信息,例如您的按钮名称,列表框名称以及您也试图传递信息的功能,我将能够为您编写更好的代码



If you give me a code extract or some more information AKA your buttons name, your list boxes name and the function you are trying to pass information too I will be able to write a better piece of code for you

>

假设您已填充ViewBag.ItemsForList = [items you want to appear on the combo box];,即美国的州名,并且在Model中具有名为MyItem的属性.如果没有该属性,则可能要使用该属性创建ViewModel类,并将其用作Model.

之后,当您执行以下操作时,将在Action中填充MyItem作为Model的属性,该属性将在提交表单时使用.

Suppose you have populated ViewBag.ItemsForList = [items you want to appear on the combo box]; i.e. Name of the States in US, and have property called MyItem in your Model. if you do not have the property then you might want to create ViewModel class with the property and use it as Model.

after this when you do following, you will have MyItem populated in your Action as property of Model used when the form is submitted.

@Html.DropDownListFor(model => model.MyItem,
    new SelectList(ViewBag.ItemsForList, "StateID", "StateName"))



您将获得在操作"中选择的州名的ID.



you will get ID of the state name selected in your Action.


这篇关于从列表框中获取所选项目并将其作为参数传递给mvc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆