Я работаю в MVC, где я пытаюсь получить доступ к данным из базы данных. Я новичок в MVC. Я получаю следующую ошибку:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'MVCDemo1.Controllers.EmployeeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
Код:
Database.SetInitializer<MVCDemo1.Models.EmployeeContext>(null);
Я добавил вышеуказанную строку в файл global.asax.cs
и получаю там ошибку.
Ниже моя модель: Employee.cs
namespace MVCDemo1.Models
{
[Table("tblEmployee")]
public class Employee
{
[Key]
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
}
}
Ниже приведен мой коддля контроллера: EmployeeController
public ActionResult Details(int id)
{
EmployeeContext employeeContext = new EmployeeContext();
Employee employee = employeeContext.Employees.Single(x => x.EmployeeId == id);
return View(employee);
}
Ниже приведен код для View: details.cshtml
@model MVCDemo1.Models.Employee
@{
ViewBag.Title = "Employee Details";
}
<h2>Employee Details</h2>
<table style="font-family:Arial">
<tr>
<td>
Employee ID:
</td>
<td>
@Model.EmployeeId
</td>
</tr>
<tr>
<td>
Name:
</td>
<td>
@Model.Name
</td>
</tr>
<tr>
<td>
Gender:
</td>
<td>
@Model.Gender
</td>
</tr>
<tr>
<td>
City:
</td>
<td>
@Model.City
</td>
</tr>
</table>
Мой маршрут configPath:
defaults: new { controller = "Employee", action = "Index", id = UrlParameter.Optional }
Может кто-нибудь просветитьмне с этой ошибкой? Любая помощь будет оценена.
Я попытался, удалив id = UrlParameter.Optional
из routeconfig, но не работал для меня.