am tring to add data to a view model, so my view can display those data but am having this issue
using (var db = new Models.PVDB01Entities())
{
var eventName = (from p in db.EventDetails where p.EventID == id select new { p.Title }).SingleOrDefault();
ViewBag.EventName = eventName;
var q = db.Events.Find(id);
Event evt = new Event();
if (q != null) {
// evt.eventDetail.Title = q.EventDetail.Title;
evt.eventDetail.Title = "sksk";
evt.eventDetail.Cateogory = q.EventDetail.Category;
evt.eventDetail.Description = q.EventDetail.Description;
evt.eventDetail.Start = q.EventDetail.StartDate;
evt.eventDetail.Stop = Convert.ToDateTime(q.EventDetail.EndDate);
evt.eventLocation.City = q.EventLocation.City;
evt.eventLocation.Country = q.EventLocation.Country;
evt.eventLocation.State = q.EventLocation.State;
evt.eventLocation.VenueAddress = q.EventLocation.VenueAddress;
evt.eventLocation.VenueName = q.EventLocation.VenueName;
return View(evt);
}
when i run this error: Object reference not set to an instance of an object.
this is the view model i want to set i have an eventDetails class , eventLocation Class, and event class, the event class combines eventDetails and eventLocation, the reason for that is because i have a view that makes use of those two class.
public class EventDetails
{
public string eventID { get; set; }
[Required]
public int Package { get; set; }
[Required(ErrorMessage = "Enter Title for address")]
public string Title { get; set; }
[Required(ErrorMessage = "Give a short description of the event ")]
public string Description { get; set; }
[Required(ErrorMessage = "Select a category")]
public string Cateogory { get; set; }
[Display(Name = "Date and Time event will Start")]
[Required(ErrorMessage = "Choose when the event will start")]
public DateTime Start { get; set; }
[Display(Name ="Date and Time event will Stop")]
public DateTime Stop { get; set; }
}
public class EventLoction
{
public string eventID { get; set; }
[Required]
public string Country { get; set; }
[Required]
public string State { get; set; }
[Required]
public string City { get; set; }
[Required(ErrorMessage ="Provide the venue name")]
[Display(Name ="Name of venue")]
public string VenueName { get; set; }
[Display(Name = "Address of venue")]
[Required(ErrorMessage ="Provide the venue address")]
public string VenueAddress { get; set; }
}
public class Event
{
public EventDetails eventDetail {get; set;}
public EventLoction eventLocation { get; set; }
}
The Error Starts at this line
evt.eventDetail.Title = "sksk";
this is the stack trace
Stack trace ` [NullReferenceException: Object reference not set to an instance of an object.]
Pluravent1._1._0.Controllers.EventController.Edit(String id) in E:\ProJects\Pluravent1.1.0\Pluravent1.1.0\Controllers\EventController.cs:129
lambda_method(Closure , ControllerBase , Object[] ) +104
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +157
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +225
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9765121
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 `