using: MVC 4, ASP.NET Razor
使用:MVC 4,ASP。净剃须刀
I'm getting an error that looks like it shouldn't be possible. It tells me that i'm using a null-reference, States, but clearly it is being set.
我得到一个错误看起来不应该是可能的。它告诉我我在使用一个null-reference, state,但是显然它正在被设置。
Controller:
控制器:
public ActionResult Index()
{
Dictionary<int, string> states = new Dictionary<int, string>()
{
{ -1, "a"},
{ 0, "b"},
{ 1, "c"},
{ 2, "d"},
};
//assigning states
ViewBag.States = states;
foreach (KeyValuePair<int, string> de in ViewBag.States)
{
Debug.WriteLine(de.Key);
}
return View();
}
The View:
视图:
<div class="search-input">
<select>
@foreach (KeyValuePair<int, string> de in ViewBag.States)
{
<option value="@de.Key">@de.Value</option>
}
</select>
</div>
The error:
错误:
Cannot perform runtime binding on a null reference
Line 54: @foreach (KeyValuePair<int, string> de in ViewBag.States)
6 个解决方案
#1
81
Found solution: I had typo in my view, ViewBag.Typo <-- this caused the error, but the debugger placed the exception at a irrelevant place.
找到解决方案:我在我的视图中有错误,ViewBag。Typo <——这导致了错误,但是调试器将异常放置在一个不相关的地方。
#2
9
This exception is also thrown when a non-existent property is being updated dynamically, using reflection.
当使用反射动态更新不存在的属性时,也会抛出此异常。
If one is using reflection to dynamically update property values, it's worth checking to make sure the passed PropertyName
is identical to the actual property.
如果使用反射来动态更新属性值,则有必要检查传递的PropertyName是否与实际属性相同。
In my case, I was attempting to update Employee.firstName
, but the property was actually Employee.FirstName
.
在我的案例中,我试图更新员工。firstName,但属性实际上是Employee.FirstName。
Worth keeping in mind. :)
值得记住的。:)
#3
7
This error happen when you have a ViewBag nonexistent in your razor code calling a method.
当您的razor代码中不存在一个ViewBag调用一个方法时,就会发生这个错误。
Controller
控制器
public ActionResult Accept(int id)
{
return View();
}
razor:
剃须刀:
<div class="form-group">
@Html.LabelFor(model => model.ToId, "To", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Flag(Model.from)
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<input value="@ViewBag.MaximounAmount.ToString()" />@* HERE is the error *@
</div>
</div>
For some reason the .net aren't able to show the error in the correct line. Normally this cause a lot of wasted time.
由于某种原因。net不能在正确的行中显示错误。通常这会导致很多时间的浪费。
#4
4
My solution to this error was that a copy and paste from another project that had a reference to @Model.Id
. This particular page didn't have a model but the error line was so far off from the actual error I about never found it!
我对这个错误的解决方案是,复制并粘贴另一个项目,该项目引用了@Model.Id。这个特定的页面没有模型,但是错误线与实际的错误相差甚远,我几乎找不到它!
#5
0
You must define states not equal to null..
你必须定义不等于零的状态。
@if (ViewBag.States!= null)
{
@foreach (KeyValuePair<int, string> de in ViewBag.States)
{
value="@de.Key">@de.Value
}
}
#6
-4
Set
集
Dictionary<int, string> states = new Dictionary<int, string>()
as a property outside the function and inside the function insert the entries, it should work.
作为一个外部的属性和函数内的函数插入条目,它应该工作。
#1
81
Found solution: I had typo in my view, ViewBag.Typo <-- this caused the error, but the debugger placed the exception at a irrelevant place.
找到解决方案:我在我的视图中有错误,ViewBag。Typo <——这导致了错误,但是调试器将异常放置在一个不相关的地方。
#2
9
This exception is also thrown when a non-existent property is being updated dynamically, using reflection.
当使用反射动态更新不存在的属性时,也会抛出此异常。
If one is using reflection to dynamically update property values, it's worth checking to make sure the passed PropertyName
is identical to the actual property.
如果使用反射来动态更新属性值,则有必要检查传递的PropertyName是否与实际属性相同。
In my case, I was attempting to update Employee.firstName
, but the property was actually Employee.FirstName
.
在我的案例中,我试图更新员工。firstName,但属性实际上是Employee.FirstName。
Worth keeping in mind. :)
值得记住的。:)
#3
7
This error happen when you have a ViewBag nonexistent in your razor code calling a method.
当您的razor代码中不存在一个ViewBag调用一个方法时,就会发生这个错误。
Controller
控制器
public ActionResult Accept(int id)
{
return View();
}
razor:
剃须刀:
<div class="form-group">
@Html.LabelFor(model => model.ToId, "To", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Flag(Model.from)
</div>
</div>
<div class="form-group">
<div class="col-md-10">
<input value="@ViewBag.MaximounAmount.ToString()" />@* HERE is the error *@
</div>
</div>
For some reason the .net aren't able to show the error in the correct line. Normally this cause a lot of wasted time.
由于某种原因。net不能在正确的行中显示错误。通常这会导致很多时间的浪费。
#4
4
My solution to this error was that a copy and paste from another project that had a reference to @Model.Id
. This particular page didn't have a model but the error line was so far off from the actual error I about never found it!
我对这个错误的解决方案是,复制并粘贴另一个项目,该项目引用了@Model.Id。这个特定的页面没有模型,但是错误线与实际的错误相差甚远,我几乎找不到它!
#5
0
You must define states not equal to null..
你必须定义不等于零的状态。
@if (ViewBag.States!= null)
{
@foreach (KeyValuePair<int, string> de in ViewBag.States)
{
value="@de.Key">@de.Value
}
}
#6
-4
Set
集
Dictionary<int, string> states = new Dictionary<int, string>()
as a property outside the function and inside the function insert the entries, it should work.
作为一个外部的属性和函数内的函数插入条目,它应该工作。