0
votes

I find some examples about this problem but I'm not able to use in my software have you got idea how to do it? And also can you explain me why I have got this problem?

No parameterless constructor defined for this object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

SpotrStore.Domein

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using SportsStore.Entities;

namespace SportsStore.Concrete
{
    public class EFDbContext : DbContext
    {
        public DbSet<Towar> Products { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SportsStore.Entities;

namespace SportsStore.Concrete
{

    namespace SportsStore.Domain.Concrete
    {
        public class EFProductRepository 
        {
            public EFDbContext context = new EFDbContext();
            public IQueryable<Towar> Towar
            {
                get { return context.Products; }
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SportsStore.Entities;

namespace SportsStore.Concrete
{
    public interface ITowarRepository
    {
        IQueryable<Towar> Towar { get; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;

namespace SportsStore.Entities
{
    [Table("Towar")]
   public class Towar
    {

       [Key]
       public int Id_tow { get; set; }
       public string Nazwa { get; set; }
       public string Opis { get; set; }
       public string Cena { get; set; }
       public int Id_kat { get; set; }

    }
}

SportStore.WebUi

    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using SportsStore.Concrete;
    using SportsStore.WebUI.Models;

    namespace SportsStore.WebUI.Controllers
    {
        public class DefaultController : Controller
        {
            public ITowarRepository repository;

            public DefaultController(SportsStore.Concrete.ITowarRepository repo)
            {
                repository = repo;

            }

            public ActionResult Index()
            {
                SomeView ViewModel = new SomeView
                {
                    Towar = repository.Towar
                    .OrderBy(p => p.Id_kat)
                };
                return View(ViewModel);
            }

        }
    }

    -----------------------------------------------

    @model SportsStore.WebUI.Models.SomeView

    @{
        ViewBag.Title = "Index";
    }

    <h2>Index</h2>

    @foreach (var item in @Model.Towar)
    {
        @item.Id_kat
    }
---------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SportsStore.WebUI.Models
{
    public class SomeView
    {
        public IEnumerable<SportsStore.Entities.Towar> Towar { get; set; }
    }
}
2

2 Answers

1
votes

By default the framework will call a parameterless constructor, which doesn't exist in your controller. A simple fix would be to add this parameterless constructor :

public DefaultController() : this(new SportsStore.Concrete.ITowarRepository()) { }
0
votes

In addition to @dombenoit's answer, since your only constructor is:

public DefaultController(SportsStore.Concrete.ITowarRepository repo)

It looks like you are trying to use dependency injection to constructor inject an instance of your data repository. So possibly your problem is that you have some dependency injection framework you are trying to use, and isn't configured correctly?