user6505433
user6505433

Reputation: 27

No parameterless constructor defined for this object nopCommerce 4.0

When I'm trying to add a view for edit data reason, it gives me the error No parameterless constructor defined for this object

This is my controller:

public class MyNewPageController : Controller
{
    MyNewPageController c = new MyNewPageController();

    public MyNewPageController()
    {

    }

    public IActionResult Index(PedroJorge.DAL.ProductDAL pd)
    {
        List<CS_mostrarModel> pList = new List<CS_mostrarModel>(pd.Read());

        return View("~/Views/MyNewPage/Index.cshtml", pList);
    }

    public ActionResult Edit(int ID)
    {
        List<CS_mostrarModel> pList = new List<CS_mostrarModel>();
        //Get the student from studentList sample collection for demo purpose.
        //You can get the student from the database in the real application
        var std = pList.Where(s => s.ID == ID).FirstOrDefault();

        return View(std);
    }

    [HttpPost]
    public ActionResult Edit(Product std)
    {
        //write code to update student 

        return RedirectToAction("Index");
    }

}

public class SampleContext : DbContext
{
    public DbSet<Order> Orders { get; set; }


    public SampleContext(DbContextOptions<SampleContext> options) : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Order>().ToTable("Order");
    }

}

Model:

 public class CS_mostrarModel
{
    public int ID { get; set; }
    public string ProductName { get; set; }

    [Display(Name = "Release Date")]
    [DataType(DataType.Date)]
    public DateTime Date { get; set; }

    [Column(TypeName = "decimal(18, 2)")]
    public int Price { get; set; }
    public int PersonsNumber { get; set; }

    public string TourType { get; set; }
    public int CarsNumber { get; set; }
}

I don't know what is wrong and I already tried everything that I saw in internet, so if someone know how to fix this please help!

Upvotes: 1

Views: 310

Answers (1)

Nkosi
Nkosi

Reputation: 247088

Unless that is a typo, then chances are that having the controller creating an instance of itself in a local field is causing a stack overflow exception when the framework is trying to activate the controller.

Each instance will try to create another instance until you run out of space on the stack.

In previous versions, the exception thrown when creating a controller was swallowed by the framework and the standard unhelpful

No parameterless constructor defined for this object

message was thrown.

Remove

 MyNewPageController c = new MyNewPageController();

from the controller and it should be able to be initialized when requested.

Upvotes: 1

Related Questions