-2

I am creating Asp.Net MVC using Ado.net data model .

Controller:

public ActionResult Create()
{
    ViewBag.Airlines = new SelectList(db.Airlines.ToList(), "Id", "Name");
    return View();

}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include="DailyPassengerId,SectorPair,FlightNumber,DepartureDate,Airline,Aircraft,Supply,POB,OccPercent,ETD,ATD,Delay")]DailyPassengerFlow dailypassengerflow)
{
    if (ModelState.IsValid)
    {
        db.DailyPassengerFlows.Add(dailypassengerflow);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(dailypassengerflow);
}

View:

@Html.Label("Airline")
    <div class="col-md-10">
        @Html.DropDownList("Airlines",(SelectList)ViewBag.Airlines)
        @Html.ValidationMessageFor(model => model.Airline)

Data is populated in dropdown list but data is not selected, i.e., I want to pass Name to this object Dailypassengerflow. I tried a lot but can't get.

Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
  • What do you mean _I want to pass Name to this object_? (what is `Name`) –  Nov 06 '17 at 07:00
  • 1
    `@Html.DropDownList("Airlines",(SelectList)ViewBag.Airlines)` => this uses `Airlines` as input name but in controller POST it only contain binding for `Airline` (without 's'). Check your naming and retry accordingly, or use `@Html.DropDownListFor(model => model.Airline, ViewBag.Airlines as SelectList)` instead. – Tetsuya Yamamoto Nov 06 '17 at 07:00
  • And why are you excluding it form binding? –  Nov 06 '17 at 07:00
  • 1
    I also suggest you read [Can the ViewBag name be the same as the Model property name in a DropDownList?](https://stackoverflow.com/questions/37161202/can-the-viewbag-name-be-the-same-as-the-model-property-name-in-a-dropdownlist) and [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Nov 06 '17 at 07:02
  • change "Airlines" to "Airline" @Html.DropDownList("Airline",(SelectList)ViewBag.Airlines)... – Jilani pasha Nov 06 '17 at 08:00
  • @StephenMuecke I mean i need to pass the description from the Airlines table which has two fields namely ID and Name. I want to pass the string from the dropdown. – Shiva Prasad Sharma Nov 06 '17 at 08:24
  • Your generating a ` –  Nov 06 '17 at 08:32
  • @StephenMuecke My problem is solved. I want to ask you one problem. I am saving single object in this case. How can I add the multiple entries in grid and finally into database. Can you please help me find some links to get my solution. – Shiva Prasad Sharma Nov 06 '17 at 08:39
  • @ShivaPrasadSharma, If you have a new question then ask a new question :). But the answers [here](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) and [here](http://stackoverflow.com/questions/37407811/mvc5-razor-html-dropdownlistfor-set-selected-when-value-is-in-array/37411482#37411482) might help you to get started –  Nov 06 '17 at 08:42

1 Answers1

0
@Html.LabelFor(model => model.Airline)
<div class="col-md-10">
@Html.DropDownListFor(model => model.Airline,ViewBag.Airlines as IEnumerable<SelectListItem>)
@Html.ValidationMessageFor(model => model.Airline)
</div>
Jilani pasha
  • 389
  • 3
  • 14