简体   繁体   English

如何从MVC的下拉菜单中获取所选项目

[英]how to get the selected item from dropdown in MVC

This is my code: 这是我的代码:

@Html.DropDownList("Locate", new List<SelectListItem>
{
    new SelectListItem {Text = "Luxor", Value="1"},
    new SelectListItem {Text = "Abu Simbel Airport", Value="2"},
    new SelectListItem {Text = "Other", Value="3"}
},"Select Location")

what I need is, when user select a list item, how to pass the selected item to controller. 我需要的是,当用户选择一个列表项时,如何将所选项目传递给控制器​​。 because data load is change according to selected list item.like below in controller. 因为数据加载根据选择的列表项而变化,例如控制器中的以下内容。

if (Value == "1")
{
    return View(cp);
}
else
{
    return View(cp1);
}

The easiest way to do achieve this is to POST data to server, where you send entire form content, along with your drop down list selection. 实现此目的的最简单方法是将数据POST到服务器,在此您发送整个表单内容以及下拉列表选择。 To achieve this, just add a submit button. 为此,只需添加一个提交按钮。 Please note that drop down list may only send simple data types. 请注意,下拉列表可能仅发送简单的数据类型。 In order to obtain this value after the POST, you need to adjust your controller like below: 为了在POST之后获得此值,您需要像下面那样调整控制器:

[HttpPost]
public void Mycontroller(string Locate)
{//do something with data}

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

相关问题 如何从GridView的Dropdown中获取所选项目? - How to get selected item from Dropdown in GridView? 如何从html下拉列表中获取所选项目并将其传递给mvc c#中的另一个控制器 - how to get the selected item from html dropdown list and pass it to another controller in mvc c# 如何从MVC C#的下拉列表中获取选定的值 - How to get selected value from dropdown list in mvc c# 如何立即获取C#MVC中下拉框所选项目的更新值? - How do I instantly get updated value of dropdown box selected item in C# MVC? 如何获得MVC下拉选择值 - How to get mvc dropdown selected value ASP.net MVC中的Enum下拉列表中的选定项目 - Selected Item in Dropdown Lists from Enum in ASP.net MVC 如何在MVC应用程序的视图中显示选定的下拉项 - How to display selected dropdown item in View of an MVC application Dropdown选定的项目在DropDownList中起作用,但在DropDownListFor中不起作用-MVC - Dropdown Selected item working in DropDownList but not in DropDownListFor - MVC 无法从控制器MVC中的下拉列表中获取选定的外键值 - Unable to get a selected foreign key value from dropdown in controller mvc 我如何从MVC下拉列表中获取选定的值作为C#中的操作链接ID - How can I get selected value from mvc dropdown list as action link id in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM