简体   繁体   English

将下拉列表中选择的值从视图传递到控制器

[英]Pass value selected in drop down list from view to controller

I am trying to pass a value that the user selects from the drop down list "COUNTRY" from a view to a controller, I tried retrieving it through an HTTP POST method and I was not successful. 我试图将用户从视图的下拉列表“ COUNTRY”中选择的值传递给控制器​​,我尝试通过HTTP POST方法检索它,但未成功。 Here is the code for my view: 这是我的看法的代码:

@using WebApplication1.Models
@using WebApplication1.Controllers
@model Country

@{
    ViewBag.Title = "Index";
}

@using (Html.BeginForm())
{
    <h2>Airport List</h2>
    @Html.Label("Airports", "Airports")
    <select name="Airports">
        @foreach (var airport in ViewBag.EuropeanAirports)
        {
            <option value="@(airport.name)">@(airport.name)</option>
        }
    </select>

    @Html.Label("Country", "Country")


    @Html.DropDownListFor(c =>c.country, new SelectList(ViewBag.countries, 
  "country", "country"), "Select Country")

}

Here is my controller: 这是我的控制器:

public class AirportController : Controller
{
    // GET: HelloWorld
    public ActionResult Create()
    {
        IEnumerable<Airport> airports = GetAirports();
        //LINQ QUERY TO RETRIEVE ALL EUROPEAN AIRPORTS 
        IEnumerable<Airport> EuropeanAirports = from n in airports
                       where n.continent.Equals("EU")
                       select n;
        IEnumerable<Country> countries = GetCountries();
        ViewBag.countries = countries;
        ViewBag.EuropeanAirports = EuropeanAirports; 
        return View(new Country());
    }

and here is my model for Country: 这是我的国家/地区模型:

public class Country
{
    public string country { get; set; }
    public string abbr { get; set; }
}

Again my goal is to retrieve the value that was selected by the user from the country drop down list. 同样,我的目标是从国家/地区下拉列表中检索用户选择的值。 I do not know whether I should add a post method for create and I have no idea how to pass the selected value from the view to the controller. 我不知道是否应添加用于创建的post方法,并且不知道如何将所选值从视图传递到控制器。

By default, an Html Helper form will post to the same controller action. 默认情况下,HTML Helper表单将发布到同一控制器操作。 You would declare it using the HttpPost method attribute like so 您可以像这样使用HttpPost方法属性声明它

[HttpPost]
public ActionResult Create(Country postedCountry)
{

    string selectedCountry = postedCountry.country

}

As you can see, the posted object will be the same model that you passed to the view, and it's properties will be determined by the form selections that the user posted 如您所见,发布的对象将与您传递给视图的模型相同,并且其属性将由用户发布的表单选择决定

  [HttpGet]
  public ActionResult Create()
    {
        IEnumerable<Airport> airports = GetAirports();
        //LINQ QUERY TO RETRIEVE ALL EUROPEAN AIRPORTS 
        IEnumerable<Airport> EuropeanAirports = from n in airports
                       where n.continent.Equals("EU")
                       select n;
        IEnumerable<Country> countries = GetCountries();
        ViewBag.countries = countries;
        ViewBag.EuropeanAirports = EuropeanAirports; 
        return View(new Country());
    }

The Name that is posted is country which is spelled in your model 发布的名称是您的模型中拼写的国家

[HttpPost]
    public ActionResult Create(string country)
        {
         if (ModelState.IsValid)//if theres not errors
            {
               //Add your save Funcation Here
               //db.Countries.Add(country)
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();//if there is errors display the same view
        }

if you need to save the Airport ddl just add the name of the ddl to the controller like so 如果您需要保存Airport ddl,只需将ddl的名称添加到控制器中,如下所示

 public ActionResult Create(string country,string Airports)

Hope this Help! 希望这个帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM