'Employee'上的'number'属性无法设置为'System.Decimal'值。您必须将此属性设置为类型为“System.Double”的非null值。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
Exception Details: System.InvalidOperationException: The 'number' property
on 'Employee' could not be set to a 'System.Decimal' value. You must set this
property to a non-null value of type 'System.Double'.
来源错误:
Line 16: {
Line 17: EmployeeContext empcon = new EmployeeContext();
Line 18: Employee employ = empcon.employees.Single(emp => emp.empid ==
id);
Line 19: return View(employ);
Line 20: }
请有人帮助我
[Table("tbl_employee")]
public class Employee
{
[Key]public int empid { get; set; }
public string name { get; set; }
public string Address { get; set;}
public double number { get; set; }
}
public class EmployeeContext :DbContext
{
public DbSet<Employee> employees { get; set; }
}
查看:
@model Employee_data.Models.Employee
@{
ViewBag.Title = "Emloyee Details";
}
<h2>Emloyee Details</h2>
<table style="font-family:Arial">
<tr>
<td>
<b>employee id</b>
</td>
<td>@Model.empid</td>
</tr>
<tr>
<td>
<b>employee Name</b>
</td>
<td>@Model.name</td>
</tr>
<tr>
<td>
<b>employee address</b>
</td>
<td>@Model.Address</td>
</tr>
<tr>
<td>
<b>employee number</b>
</td>
<td>@Model.number</td>
</tr>
</table>
@Html.ActionLink("click me", "Details", new { id = "1" })
Global.asax中:
protected void Application_Start()
{
Database.SetInitializer<Employee_data.Models.EmployeeContext>(null);
}
控制器:
public ActionResult Details(int id)
{
EmployeeContext empcon = new EmployeeContext();
Employee employ = empcon.employees.Single(emp => emp.empid == id);
return View(employ);
}
web.config
:
<connectionStrings>
<add name="EmployeeContext"
connectionString="Data Source=server;Initial Catalog=Testdb;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
路线配置:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteTable.Routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "emp", action = "Details",
id = UrlParameter.Optional }
);
}
答案 0 :(得分:0)
如果您尝试调用此控制器操作,则会抛出此错误 您不在路径部分或查询中指定ID 字符串参数。由于您的控制器操作采用id为 参数应确保始终指定此参数。
确保在您申请此操作时已指定 url中的有效ID:
如果empid
是您的关键,请尝试使用
[Key] public int empid{ get; set; }