简体   繁体   中英

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. 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.

By default, an Html Helper form will post to the same controller action. You would declare it using the HttpPost method attribute like so

[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

 public ActionResult Create(string country,string Airports)

Hope this Help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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