I Got an error While submit “Object reference not set to an instance of an object” mvc2
View page
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<EventListing.Models.EventInfo>" %>
<%= Html.DropDownListFor(model => model.SelectedTimeZone, Model.TimeZones, "Select Timezone") %>
Controller
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(EventInfo EventInfo)
{
try
{
if (ModelState.IsValid)
{
EventModel.Create(EventInfo);
return RedirectToAction("Index");
}
return View();
}
catch
{
return View();
}
}
Model
public SelectList TimeZones { get; set; }
private string selectedTimeZone = "";
public string SelectedTimeZone
{
get { return selectedTimeZone; }
set { selectedTimeZone = value; }
}
public static IList<KeyValuePair<string, string>> getTIMEZOMES
{
get
{
Dbhelper DbHelper = new Dbhelper();
IList<KeyValuePair<String, String>> Timezone = new List<KeyValuePair<String, String>>();
DbCommand cmd = DbHelper.GetSqlStringCommond("SELECT * FROM TMP_TIMEZONES");
DbDataReader Datareader = DbHelper.ExecuteReader(cmd);
while (Datareader.Read())
{
Timezone.Add(new KeyValuePair<String, String>(Datareader["ABBR"].ToString(), Datareader["NAME"].ToString()));
}
return Timezone;
}
}
Plz give me the solution.
You need to repopulate the data for the dropdown in the [HttpPost] parts of the
Createaction just like I’m guessing you are doing in the GET version of yourCreateaction. When you do areturn View(Model)in the HttpPost action it literally returns that HTML view and it needs all the supporting data too just like in the GET!So at least two errors: