我找到了一些关于这个问题的例子,但我无法在我的软件中使用你知道怎么做吗?你能解释一下我为什么会遇到这个问题吗?
没有为此对象定义无参数构造函数。说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.MissingMethodException:没有为此对象定义无参数构造函数。
SpotrStore.Domain
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; }
}
}