'Relationship with new property in model
Ive added a new property
public List<string> DeclinedMembers { get; set; }
I deleted my database and re-initialized it. but when try to add somethiung to the list in the controller i keep getting this exception thrown at the DeclinedMembers line.
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Scheduler.Models.Event.DeclinedMembers.get returned null.
Why is it not able to find this new property? Did i miss a step? See below for model and controller
Model:
public class Event
{
[Required]
public int EventId { get; set; }
[ForeignKey("UserId")]
public virtual SchedulerUser SchedulerUser { get; set; }
[MaxLength(50)]
public string EventCreator { get; set; }
public List<string> EventMembers { get; set; }
public List<string> DeclinedMembers { get; set; }
[Required]
[MaxLength(100)]
public string Subject { get; set; }
[MaxLength(400)]
public string Description { get; set; }
[Required]
public DateTime StartTime { get; set; }
public DateTime? EndTime { get; set; }
[Required]
public bool IsFullDay { get; set; }
[Required]
public bool AcceptOrDecline { get; set; }
}
Controller(create):
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("EventId,UserId,EventCreator,EventMembers,DeclinedMembers,Subject,Description,StartTime,EndTime,IsFullDay,AcceptOrDecline")] Event @event)
{
if (ModelState.IsValid)
{
@event.SchedulerUser = await _userManager.GetUserAsync(HttpContext.User); //This also sets the UserId for the event.
var userName = @event.SchedulerUser.UserName;
@event.EventCreator = userName;
@event.AcceptOrDecline = true;
@event.DeclinedMembers.Add("foo");
_context.Add(@event);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(@event);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
