c# - Model value is null but why? -
i've been working on web application uni handle travel bookings in c# , have have hit brick wall trying overcome issue.
when attempting open webpage 'flightdb', receive following error:
an exception of type 'system.nullreferenceexception' occurred in app_web_z0gc0uw3.dll not handled in user code in flightdb's view, value of model null. i'm causing error, can't understand why.
here's view:
@model ienumerable<websiteassignment.models.flightstbl> @{ viewbag.title = "choose flights:"; } <h2>flightdb</h2> <table> @foreach (var item in model) //model = null { <tr> <td> @html.displayfor(modelitem => item.flightid) </td> <td> @html.displayfor(modelitem => item.departing_airport) </td> <td> @html.displayfor(modelitem => item.arriving_airport) </td> <td> @html.displayfor(modelitem => item.departure_flight_time) </td> <td> @html.displayfor(modelitem => item.return_flight_time) </td> <td> @html.displayfor(modelitem => item.available_space) </td> </tr> } </table> and controller:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using websiteassignment.models; namespace websiteassignment.controllers { public class homecontroller : controller { // // get: /home/ private flightdbentities1 db = new flightdbentities1(); public actionresult index() { int currenthour = datetime.now.hour; viewbag.message = currenthour < 12 ? "good morning" : "good afternoon"; return view(db.flightstbls.tolist()); } [httpget] public actionresult applicationpage() { return view(); } [httppost] public actionresult applicationpage(application newapplication) { var websiteassignment = new list<application>(); if (session["websiteassignment"] != null) { websiteassignment = (list<application>)session["websiteassignment"]; } websiteassignment.add(newapplication); session["websiteassignment"] = websiteassignment; if (modelstate.isvalid) { return redirecttoaction("flightdb"); //return redirecttoaction("index"); } else { return view(); } } public actionresult flightdb() { return view(); } } } if please me understand extremely grateful. pulling hair out on this.
thanks in advance help.
you must pass in model model have value. example:
public actionresult flightdb() { return view(new list<websiteassignment.models.flightstbl>()); } view() has number of overloads, 1 of allows pass in model. mvc framework search view matching name of method (flightdb, in case) send model rendering.
Comments
Post a Comment