C# 无法对空引用执行运行时绑定,但它不是空引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19457804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot perform runtime binding on a null reference, But it is NOT a null reference
提问by generalcoder
using: MVC 4, ASP.NET Razor
使用:MVC 4,ASP.NET Razor
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.
我收到一个错误,看起来不可能。它告诉我我正在使用空引用状态,但显然它正在设置。
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)
回答by Alexandre
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.
作为函数外部的属性和函数内部插入条目,它应该可以工作。
回答by generalcoder
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 <-- 这导致了错误,但调试器将异常放置在不相关的地方。
回答by XtraSimplicity
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
.
就我而言,我试图更新Employee.firstName
,但该属性实际上是Employee.FirstName
.
Worth keeping in mind. :)
值得牢记。:)
回答by Cuteboy_Max
You must define states not equal to null..
您必须定义不等于 null 的状态。
@if (ViewBag.States!= null)
{
@foreach (KeyValuePair<int, string> de in ViewBag.States)
{
value="@de.Key">@de.Value
}
}
回答by AFetter
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 无法在正确的行中显示错误。通常这会造成大量时间浪费。
回答by Eonasdan
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 havea model but the error line was so far off from the actual error I about never found it!
我对此错误的解决方案是从另一个项目复制并粘贴到@Model.Id
. 这种特殊页面没有有一个模型,但错误行是如此遥远,从实际的错误,我从来没有关于发现它!