我在尝试使用强类型模型创建视图时遇到问题.无论我将什么作为模型传递给 View()
,即使只是访问 Model
,我总是会收到 NullReferenceException
.p>
在执行剃刀页面的其余部分之前,我什至无法检查模型是否为空;简单地执行 if (Model != null)
也会抛出相同的 NullReferenceException
.
Index.cshtml
model 编码模型@{布局 = "~/Pages/Shared/_Layout.cshtml";}<h2>编码</h2><div id="进度">@await Html.PartialAsync("~/Encoder/MVC/EncodeProgress.cshtml", new EncodeModule())</div>
EncodeProgress.cshtml
model FFenc.IEncoderModule@{var模块=模型;//这会抛出 NullReferenceException}
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env){if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}别的{app.UseExceptionHandler("/错误");应用程序.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();app.UseCookiePolicy();app.UseMvc();}
异常堆栈跟踪:
<块引用>NullReferenceException:对象引用未设置为对象的实例.
AspNetCore.Encoder_MVC_EncodeProgress.get_Model()
EncodeProgress.cshtml 中的 AspNetCore.Encoder_MVC_EncodeProgress.ExecuteAsync()
var 模块 = 模型;
我做错了什么?我尝试了多种修复和解决方法(使用 ViewComponent 而不是视图),但没有任何效果.
我发现的一些类似问题并没有解决我的问题:
ASP.NET Core Model null 错误索引视图
我已经传递了模型,所以这个答案不会改变我正在做的任何事情.例如,当我尝试使用控制器作为解决方法时,同样的 NullReferenceException
发生在这段代码中:
[Route("/encode/progress")]公共 IActionResult GetProgress(){return View("~/Encoder/MVC/EncodeProgress.cshtml", new EncoderModule());}
我认为你在混合 Razor 页面 与 ASP.NET MVC.尝试删除 @page
指令,您的模型应该绑定到调用 return View()
时传递的值.
I am having trouble with attempting to create a view with a strongly typed model. No matter what I pass in as the model to a View()
, I always receive a NullReferenceException
when even just accessing the Model
.
I can't even check if the model is null before executing the rest of the razor page; simply doing a if (Model != null)
also throws the same NullReferenceException
.
Index.cshtml
@page
@model EncodeModel
@{
Layout = "~/Pages/Shared/_Layout.cshtml";
}
<h2>Encode</h2>
<div id="progress">
@await Html.PartialAsync("~/Encoder/MVC/EncodeProgress.cshtml", new EncodeModule())
</div>
EncodeProgress.cshtml
@page
@model FFenc.IEncoderModule
@{
var module = Model; //this throws the NullReferenceException
}
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc();
}
Exception stack trace:
NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Encoder_MVC_EncodeProgress.get_Model()
AspNetCore.Encoder_MVC_EncodeProgress.ExecuteAsync() in EncodeProgress.cshtml
var module = Model;
What am I doing wrong? I have attempted multiple fixes and workarounds (using a ViewComponent instead of a view) but nothing works.
Some similar questions that I have found that have not solved my problem:
ASP.NET Core Model null error in the Index view
I'm already passing in the model so this answer doesn't change anything about what I'm doing. For example, when I was trying to use a controller as a workaround, the same NullReferenceException
happened with this code:
[Route("/encode/progress")]
public IActionResult GetProgress()
{
return View("~/Encoder/MVC/EncodeProgress.cshtml", new EncoderModule());
}
I think you're mixing Razor Pages with ASP.NET MVC. Try removing the @page
directive and your model should be bound to the value passed when you call return View()
.
这篇关于仅访问模型时的 ASP.NET Core NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!