2

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.

4
  • 1
    What kind of class is PageModel ? Commented Oct 4, 2019 at 3:57
  • Your ChatModal class is supposed to have default parameterless constructor only as far as I know Commented Oct 4, 2019 at 4:04
  • Why is the ChatModel a PageModel? Seems like you're mixing RazorPages and Mvc in a strange way Commented Oct 4, 2019 at 6:54
  • I am new to Razor pages, are the razor page models not interchangeable with MVC models? Commented Oct 4, 2019 at 17:18

2 Answers 2

5

You should not combine PageModel and Controller.

As the error indicates, Model bound complex types must not be abstract or value types and must have a parameterless constructor. For ChatModel which inherits PageModel is abstract class.

Create a new separate class for use in Controller action.

Sign up to request clarification or add additional context in comments.

Comments

-1
using Microsoft.AspNetCore.Mvc;

namespace sucu1.Models
{
    public class LoginViewModel : Controller
    {

        public string? Mail { get; set; }
        public string? Password { get; set; }
    }
}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.