0

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; }
    }
}
RPD
  • 494
  • 3
  • 8
  • 24

2 Answers2

1

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()) { }
dom
  • 6,702
  • 3
  • 30
  • 35
  • When I`m trying do it this -----> public DefaultController(SportsStore.Concrete.ITowarRepository repo) : this(new SportsStore.Concrete.ITowarRepository()) { repository = repo; } "I have got error" Error 1 Cannot create an instance of the abstract class or interface 'SportsStore.Concrete.ITowarRepository' C:\Users\Rafal\Desktop\MVC 3 ksiązka\Przygotowanie do Pracy\SportsStore.WebUI\Controllers\DefaultController.cs 15 85 SportsStore.WebUI – RPD Sep 28 '12 at 18:48
  • Well whatever class you are trying to pass an instance of to the constructor is what you should be using as a parameter, I just put `new SportsStore.Concrete.ITowarRepository()` in there because that was the type used in the code you provided. Obviously you need to use an instance of the appropriate class, and not the interface. – dom Sep 28 '12 at 18:52
0

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?

CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138
  • At the beginning I take data in my constructor later I make some changes in Index action method (LINQ) later I put this data to class SomeView() and there are my filtered data – RPD Sep 28 '12 at 18:54