0

I am trying to create a drop down list in MVC4 derived from a database model called 'SiteTableBookings', the runtime error is:

"An exception of type 'System.NullReferenceException' occurred in App_Web_e3cawdyt.dll but was not handled in user code"

Basically the application requires a drop down list for bookings of flights, I have searched many methods on how to achieve a drop down list online and have not been able to get the application to run.

New to MVC4 and still learning any help would be much appreciated.

Model:

public partial class SiteBookingsTable
{
    public int listID { get; set; }
    public string departureAirport { get; set; }  
    public string chooseDepartureAirport { get; set; }     

    public IEnumerable<SiteBookingsTable> selectDeparture = new List<SiteBookingsTable>
    {
        new SiteBookingsTable {listID = 0, departureAirport = "London (LTN)"},
        new SiteBookingsTable {listID = 1, departureAirport = "Manchester (MAN)"}
    };
}

View:

<tr>
   <td>@Html.LabelFor(model => model.chooseDepartureAirport)<br /> 
       @Html.DropDownListFor(model => model.chooseDepartureAirport,
        new SelectList(Model.selectDeparture, "listID", "departureAirport")) </td>
</tr>

Controller:

public ActionResult Create()
{           
    return View();
}

2 Answers 2

1

I would create the list in the controller rather than the model.

Model:

public partial class SiteBookingsTable
{
    public int listID { get; set; }
    public string departureAirport { get; set; }  
    public string chooseDepartureAirport { get; set; }     

    public IEnumerable<SelectListItem> selectDeparture { get; set; }
}

View:

<tr>
   <td>@Html.LabelFor(model => model.chooseDepartureAirport)<br /> 
       @Html.DropDownListFor(model => model.chooseDepartureAirport,
                             Model.selectDeparture)
   </td>
</tr>

Controller:

public ActionResult Create()
{
    IEnumerable<SiteBookingsTable> selectDeparture = new List<SiteBookingsTable>()
    {
        new SiteBookingsTable {listID = 0, departureAirport = "London (LTN)"},
        new SiteBookingsTable {listID = 1, departureAirport = "Manchester (MAN)"}
    };

    model = new SiteBookingsTable()
    model.selectDeparture = new SelectList(selectDeparture, "listID", "departureAirport");

    return View(model);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think you need to make the selectDeparture field static like so:

public static IEnumerable<SiteBookingsTable> selectDeparture = new List<SiteBookingsTable> 
{ 
    new SiteBookingsTable { listID = 0, departureAirport = "London (LTN)" }, 
    new SiteBookingsTable { listID = 1, departureAirport = "Manchester (MAN)" } 
};

And then update your dropdown list to use this static property, and to point at the departureAirport property:

@Html.DropDownListFor(model => model.departureAirport,
    new SelectList(SiteBookingsTable.selectDeparture, "listID", "departureAirport"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.