'No parameterless constructor defined for this object - asp.net mvc
https://www.youtube.com/watch?v=s3o8iuoDMyI&t=371s&ab_channel=C%23CodeAcademy
So I was following this guide but came across the problem of "No parameterless constructor defined for this object".
The solutions that I've been seeing is that I need to use a dependency injection, but I'm still struggling to understand it.
This is my code:
//HomeController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Judging_System.Models;
using System.Data.SqlClient;
using Judging_System.Controllers;
using Microsoft.Extensions.Logging;
namespace Judging_System.Controllers
{
public class HomeController : Controller
{
SqlConnection con = new SqlConnection();
SqlCommand com = new SqlCommand();
SqlDataReader dr;
private readonly ILogger<HomeController> _logger;
List<Events> events = new List<Events>();
// GET: Home
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
con.ConnectionString = Judging_System.Properties.Resources.ConnectionString;
}
[HttpGet]
public ActionResult Index()
{
return View();
}
private void FetchData()
{
if(events.Count > 0)
{
events.Clear();
}
try
{
con.Open();
com.Connection = con;
com.CommandText = "Select top (1000) [Id], [Event], [Venue], [Date], [Time] from [tbl_Events].[Id]";
dr = com.ExecuteReader();
while (dr.Read())
{
events.Add(new Events() { Id = dr["Id"].ToString(),
Event = dr["Event"].ToString()
,Venue = dr["Venue"].ToString()
,Date = dr["Date"].ToString()
,Time = dr["Time"].ToString()
});
}
con.Close();
}
catch (Exception ex)
{
throw ex;
}
}
[HttpPost]
public ActionResult Contestants()
{
return View();
}
public ActionResult Latest_Events()
{
return View();
}
public ActionResult Past_Events()
{
return View();
}
public ActionResult Calendar()
{
return View();
}
public ActionResult Reports()
{
return View();
}
//Under Maintenance
public ActionResult User_Account()
{
return View();
}
public ActionResult Events(Events events)
{
FetchData();
con.Open();
com.Connection = con;
com.CommandText = "select * from tbl_Events where Id='" + events.Id + "'and Event='" + events.Event +
"'and Venue='"+ events.Venue + "'and Date='" + events.Date + "'and Time='" + events.Time +"'";
dr = com.ExecuteReader();
return View(events);
}
public ActionResult Criteria_For_Judging()
{
return View();
}
public ActionResult Judges()
{
return View();
}
public ActionResult Participants()
{
return View();
}
public ActionResult Pointing_System()
{
return View();
}
}
}
//Events.cshtml
@{
ViewData["Title"] = "Events";
Layout = "~/Views/_HomeLayoutPage.cshtml";
string[] TableHeaders = new string[] { "Id", "Events", "Venue", "Date", "Time" };
}
<div class="table">
<table class="table table-bordered table-hover">
<thead>
<tr>
@{
foreach (var head in TableHeaders)
{
<th>
@head
</th>
}
}
</tr>
</thead>
<tbody>
@{
if (Model != null)
{
foreach (var Data in Model)
{
<tr>
<td>@Data.Event</td>
<td>@Data.Venue</td>
<td>@Data.Date</td>
<td>@Data.Time</td>
</tr>
}
}
}
</tbody>
</table>
</div>
Thanks for the help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
