简体   繁体   English

如何在asp.net mvc3中获取下拉列表的选定值?

[英]how to get selected value of drop down list in asp.net mvc3?

This is my code Display data from the database into DropDownList in asp.net MVC3 In this I can now display data from database into dropdownlist but now I want that if I select any thing from dropdownlist data I want that selected value pass in my stored procedure. 这是我的代码在asp.net MVC3中将数据库中的数据显示到DropDownList中,现在我可以将数据库中的数据显示到dropdownlist中,但是现在我希望,如果我从dropdownlist数据中选择任何东西,我希望该选定值在我的存储过程中传递。 I know there is same question might be available or has already been asked but I am not getting my answer which I want. 我知道可能有相同的问题,或者已经有人问过,但是我没有得到想要的答案。 If anyone can explain me step by step or provide me some useful links. 如果有人可以逐步向我解释或提供一些有用的链接。

This code is not completly tested and implemented. 此代码尚未完全测试和实现。

You can acheive it using onchange event of dropdownlist . 您可以使用dropdownlist onchange事件来实现它。

@Html.DropDownListFor(x => x.SelectedItemValue,new {@onchange="passvalue(this);"})

Handle it using javascript and ajax 使用javascript和ajax处理它

<script type="text/javascript">
  function passvalue(e){
      $.ajax({
         type: 'POST',
         dataType: 'json',
         url: '@Url.Action("Index", "Home")',
         data: ({ selectedValue: e.innerHTML }),
         success: function (result) {
                                    },
         error: function (result) {
                    alert('error');
                }
      });
  }
  </script>

Now you can get the selected value to the controller 现在您可以将选择的值发送给控制器

[HttpPost]
public ActionResult Index(int selectedValue)
{
   // do whatever you want with `selectedValue`
}

Hope it helps 希望能帮助到你

This is a way to achieve this: 这是一种实现此目的的方法:

Subscribe the change event of the dropdown and call the action method(with ajax for example) that passes the selected value to the stored procedure. 订阅下拉列表的change事件,并调用将所选值传递到存储过程的action方法(例如,使用ajax )。

There are several approaches. 有几种方法。 One of the simplest is to have a ViewModel with a collection of SelectListItems to render in the drop-down and an item to take the selection. 最简单的方法之一是拥有一个ViewModel,其中包含要在下拉列表中呈现的SelectListItems集合,以及一个要进行选择的项目。 In my example code below of course I am using hard-coded values etc to demo the point! 在下面的示例代码中,我当然使用了硬编码的值等来演示这一点!

ViewModel 视图模型

public class TestViewModel
{
    public IEnumerable<SelectListItem> TestListItems
    {
        get
        {
            return new List<SelectListItem>
            {
                new SelectListItem { Text = "Item 1", Value = "1" },
                new SelectListItem { Text = "Item 1", Value = "1" },
                new SelectListItem { Text = "Item 1", Value = "1" },
            };
        }
    }

    public string SelectedItemValue { get; set; }
}

View 视图

<p>@Html.DropDownListFor(x => x.SelectedItemValue, new SelectList(Model.TestListItems, "Value", "Text"))</p>

Controller 控制者

public ActionResult Index()
{
    var viewModel = new TestViewModel();
    return View(viewModel);
}

[HttpPost]
public ActionResult Index(TestViewModel viewModel)
{
    string youChose = viewModel.SelectedItemValue;
    // ...
}

Also, to highlight the collection doesn't have to be of SelectListItem and could be another type. 另外,要突出显示该集合,不必一定是SelectListItem而可以是另一种类型。 Then all you would need to do is change your view to perform a .Select to convert into a SelectListItem . 然后,您所需要做的就是更改视图以执行.Select转换为SelectListItem For simplicity I've just made the ViewModel use it directly. 为简单起见,我只使ViewModel直接使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.NET MVC3中创建下拉列表? - How to create a drop down list in ASP.NET MVC3? ASP.NET MVC 2-如何从下拉菜单中获取选定的值? - ASP.NET MVC 2 - How can I get the selected value from a drop down into my view model? 如何获取转换后的布尔值以在asp.net gridview的可编辑下拉列表中显示为选定值? - How to get converted boolean value to show as selected value in the editable drop down list in asp.net gridview? 获取下拉列表默认所选项目未传递到Asp.net MVC中的控制器 - Get drop down list default selected item is not passing to the controller in Asp.net MVC asp.net:设置为下拉列表选择的默认值 - asp.net : set default value selected for a drop down list 无法在asp.net的下拉列表中显示选定的值 - Not able to display selected value in drop down list in asp.net 如何确定选定的值在asp.net MVC视图中的下拉列表? - How to determine selected value for drop down lists in asp.net MVC View? ASP.NET 代码隐藏无法获取选定的下拉列表值(设置 Javascript 属性后) - ASP.NET codebehind can't get selected drop down list value (after set Javascript attribute) 如何在asp.net中保存下拉列表选择的索引? - How to save the drop down list selected index in asp.net? 获取下拉列表用于在ASP.NET MVC中显示选定的值 - Get the drop downListFor show the selected value in asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM