I'm working on a chating site and I've been getting this pesky error for a while now, really don't know what it could be. I have this Model class:
public class ChatModel : PageModel
{
public string UserName { get; set; }
public ICollection<string> Espectators { get; set; }
public ICollection<string> Chatters { get; set; }
public string Mediator { get; set; }
public Conversation Conversation{ get; set; }
public Lazy<IConcurrentCaching> _cache { get; set; }
public ChatModel()
{
}
public ChatModel(string connString, string bulkTime, string username, ICollection<string> espectators = null, ICollection<string> chatters = null, string mediator = null, string conversationName = null)
{
//build the class, boring stuff
}
public void OnGet()
{
}
}
This model is part of a Razor page. This is my controller for the Chat view:
public class ChatController: Controller
{
public ChatController()
{
}
public IActionResult Chat(ChatModel model)
{
return View(model);
}
}
And the call for the Controller action:
public IActionResult CreateChat(string username, string conversationName)
{
if (_cache.Value.Get<List<string>>("ChatList") == null)
{
_cache.Value.Set<List<string>>("ChatList", new List<string>(), 1440);
}
ChatModel model = new ChatModel(_op.Value.ConnectionString, _op.Value.BulkLoadCacheTimeInMinutes, username, conversationName: conversationName);
model.Chatters.Add(username);
return RedirectToAction("Chat","Chat", model);
}
For some reason, when I call the controller method for the Chat view, it throws the following exception:
InvalidOperationException: Could not create an instance of type 'Microsoft.AspNetCore.Http.HttpContext'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Alternatively, set the 'HttpContext' property to a non-null value in the 'Chatter.UI.Web.Views.Chat.ChatModel' constructor. Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext bindingContext)
The weirdest thing is that I'm not even using HttpContext on the ChatModel class. I have tried using DI to inject the IHttpContextAccessor on my class (even though I don't see how that would help, but I've seen that as a solution online); it did't work. I've also tried to add a singleton on the startup for the HttpContext, but to no avail. Any ideas on what is going on would be greatly appreciated.
ChatModalclass is supposed to have default parameterless constructor only as far as I know