sgt*_*boy 23 c# asp.net-mvc nullreferenceexception razor asp.net-mvc-5
出于某种原因,每当我尝试访问我的模型时,我都会收到NullReferenceException.
这是我的控制器的代码:
public async Task<ActionResult> Bar(string fooSlug, int barId)
{
var foo = await mediaService.GetFoo(fooSlug);
var bar = await barService.GetBarFromFooByTitleId(foo.TitleId, barId);
var viewModel = new ViewModels.BarViewModel(foo, bar);
return View(viewModel);
}
Run Code Online (Sandbox Code Playgroud)
ViewModel中的代码:
public class BarViewModel
{
public Models.Sub.FooDetails Foo{ get; set; }
public Models.Sub.BarDetails Bar { get; set; }
public BarViewModel(Models.Sub.FooDetails foo, Models.Sub.BarDetails bar)
{
this.Foo = foo;
this.Bar = bar;
}
}
Run Code Online (Sandbox Code Playgroud)
我的观点:
@model FooBar.ViewModels.BarViewModel
@{
ViewBag.Title = "Bar";
}
<h2>@Model.Bar.Name</h2>
Run Code Online (Sandbox Code Playgroud)
它继续返回NullReferenceException当我尝试在h2标记内使用它时.我调试了它,.Name属性具有正确的值,但是当我按下继续时它只会抛出错误.
有没有人有这个问题的解决方案?
San*_*mar 45
有时编译器无法指向剃刀视图中具有特定类型错误的确切行,可能是因为它无法将其行号保留在堆栈跟踪或某处.我在Null Reference Exception中找到了这种情况,并且在Url.Content中传递了null.
因此,当您在堆栈跟踪显示的行上没有出现任何错误时,检查剃刀视图中的下一个C#语句会有所帮助.
| 归档时间: |
|
| 查看次数: |
12073 次 |
| 最近记录: |