I am getting NullReferenceException when I post the form back to the server. I am using ADO.NET with ASP.NET Core 3
Below is the Model for Campgrounds:
public class Campground
{
public int ID { get; set; }
[Required]
[MaxLength(200)]
[Display(Name = "Campground Name")]
public string CampgroundName { get; set; }
[Required]
public int State { get; set; }
public List<State> States { get; set; }
[Required]
public int Peak { get; set; }
public List<Month> Months { get; set; }
[Required]
public string Overview { get; set; }
[Required]
[Display(Name = "Season Date From")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/YYYY}")]
[DataType(DataType.Date)]
public DateTime? SeasonDateFrom { get; set; }
[Required]
[Display(Name = "Season Date To")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/YYYY}")]
[DataType(DataType.Date)]
public DateTime SeasonDateTo { get; set; }
[Display(Name = "Natural Features")]
public string NaturalFeatures { get; set; }
public string Recreation { get; set; }
public string Facilities { get; set; }
[Required]
[Display(Name = "Campground Photo")]
public string ImagePath { get; set; }
}
I also have created a ViewModel and I called it CampgroundDetails:
public class CampgroundDetails : Campground
{
[Required]
[Display(Name = "Campground Image")]
public IFormFile Photo { get; set; }
}
I have tried different approaches but I am not able to move on because of this error. Your help is much appreciated.
Here is the HttpGet of Create Method:
[HttpGet]
public IActionResult Create()
{
CampgroundDetails campgroundDetails = new CampgroundDetails();
List<State> states = new List<State>();
List<Month> months = new List<Month>();
using (SqlConnection connection = new SqlConnection(ConnectionString.DBCS))
{
using (SqlCommand command = new SqlCommand("sp_GetStates_Months", connection))
{
command.CommandType = CommandType.StoredProcedure;
if (connection.State == ConnectionState.Closed)
connection.Open();
using (SqlDataAdapter ada = new SqlDataAdapter(command))
{
DataSet ds = new DataSet();
ada.Fill(ds);
DataTable statesDataTable = ds.Tables[0];
foreach(DataRow row in statesDataTable.Rows)
{
states.Add(new State()
{
ID = Convert.ToInt32(row["ID"]),
StateName = row["StateName"].ToString()
});
}
campgroundDetails.States = states;
DataTable monthsDataTable = ds.Tables[1];
foreach(DataRow row in monthsDataTable.Rows)
{
months.Add(new Month()
{
ID = Convert.ToInt32(row["ID"]),
MonthName = row["Month"].ToString()
}); ;
}
campgroundDetails.Months = months;
}
}
}
return View(campgroundDetails);
}
Here is the HttpPost of Create Method:
[HttpPost]
public IActionResult Create(CampgroundDetails campgroundDetails)
{
if (ModelState.IsValid)
{
string filePath = UploadFile(campgroundDetails);
using (SqlConnection connection = new SqlConnection(ConnectionString.DBCS))
{
using (SqlCommand command = new SqlCommand("sp_InsertNewCampground", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@CampgroundName", campgroundDetails.CampgroundName);
command.Parameters.AddWithValue("@State", campgroundDetails.State);
command.Parameters.AddWithValue("@Peak", campgroundDetails.Peak);
command.Parameters.AddWithValue("@SeasonDateFrom", campgroundDetails.SeasonDateFrom);
command.Parameters.AddWithValue("@SeasonDateTo", campgroundDetails.SeasonDateTo);
command.Parameters.AddWithValue("@NaturalFeatures", campgroundDetails.NaturalFeatures ?? "");
command.Parameters.AddWithValue("@Recreation", campgroundDetails.Recreation ?? "");
command.Parameters.AddWithValue("@Facilities", campgroundDetails.Facilities ?? "");
command.Parameters.AddWithValue("@ImagePath", filePath);
command.Parameters.AddWithValue("@Overview", campgroundDetails.Overview);
if (connection.State == ConnectionState.Closed)
connection.Open();
command.ExecuteNonQuery();
}
}
return RedirectToAction("index", "home");
}
return View();
}
Here is the error I am getting: