当我尝试出于编辑数据原因添加视图时,它给了我错误No parameterless constructor defined for this object
这是我的控制器:
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");
}
}
模型:
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; }
}
我不知道出了什么问题,我已经尝试了我在互联网上看到的所有内容,所以如果有人知道如何解决这个问题,请帮忙!