我有以下MVC
型号:
Public Class Employee
Public Property EmployeeID As Integer
End Class
控制器:
Namespace Controllers
Public Class EmployeeController
Inherits Controller
Function Details() As ActionResult
Dim employee As Employee
employee = New Employee
employee.EmployeeID = 101
Return View()
End Function
End Class
End Namespace
查看:
@ModelType MVCDemo.Employee
@Code
ViewData("Title") = "Employee Details"
End Code
<h2>Employee Details</h2>
<table style="font-family:Arial">
<tr>
<td>
@Model.EmployeeID
</td>
</tr>
</table>
我的问题: 运行代码我收到以下错误:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
对于这一行:
第11行:@ Model.EmployeeID
我只是不知道为什么没有对象引用。我在剃刀代码中得到了模型的智能感知。初始化并将值分配给属性是可以的。 Mybe我只是视而不见,任何人都可以帮助我,为什么这不起作用?
答案 0 :(得分:1)
您需要将模型实例传递给视图:
Function Details() As ActionResult
Dim employee As Employee
employee = New Employee
employee.EmployeeID = 101
Return View(employee)
End Function