gen*_*der 67 c# asp.net asp.net-mvc razor
使用:MVC 4,ASP.NET Razor
我收到的错误看起来应该是不可能的.它告诉我,我正在使用空引用,状态,但很明显它正在被设置.
控制器:
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();
}
Run Code Online (Sandbox Code Playgroud)
风景:
<div class="search-input">
<select>
@foreach (KeyValuePair<int, string> de in ViewBag.States)
{
<option value="@de.Key">@de.Value</option>
}
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
错误:
Cannot perform runtime binding on a null reference
Line 54: @foreach (KeyValuePair<int, string> de in ViewBag.States)
Run Code Online (Sandbox Code Playgroud)
gen*_*der 92
找到解决方案:我的视图中有拼写错误,ViewBag.Typo < - 这导致错误,但调试器将异常置于不相关的位置.
AFe*_*ter 13
当您的剃刀代码中调用方法时ViewBag不存在时,会发生此错误.
调节器
public ActionResult Accept(int id)
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
剃刀:
<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>
Run Code Online (Sandbox Code Playgroud)
由于某种原因,.net无法在正确的行中显示错误.通常这会浪费很多时间.
Xtr*_*ity 12
使用反射动态更新不存在的属性时,也会抛出此异常.
如果使用反射来动态更新属性值,则值得检查以确保传递PropertyName与实际属性相同.
在我的情况下,我试图更新Employee.firstName,但实际上属性Employee.FirstName.
值得记住.:)
小智 -4
放
Dictionary<int, string> states = new Dictionary<int, string>()
Run Code Online (Sandbox Code Playgroud)
作为函数外部的属性并在函数内部插入条目,它应该可以工作。
| 归档时间: |
|
| 查看次数: |
107552 次 |
| 最近记录: |