[英]ASP.NET MVC Helper Methods Throwing Null Reference Exception
I have been working on a project for about 9 months now using .NET Framework ASP.NET MVC.我已经使用 .NET Framework ASP.NET MVC 在一个项目上工作了大约 9 个月。 I have a view with many forms each with its own model.
我有许多表格的视图,每个表格都有自己的模型。 I use a ViewModel that contains all the individual models that I am using for the forms.
我使用一个 ViewModel,其中包含我用于表单的所有单个模型。 I use the Html Helpers to create input boxes such as TextBoxFor and TextAreaFor in the forms as shown below.
我使用 Html Helpers 以如下所示的形式创建输入框,例如 TextBoxFor 和 TextAreaFor。
@Html.TextAreaFor(d => d.Foo.Property, new { @class = "form-control" })
This has worked since starting the project many months ago and continues to work in production, however as of yesterday when trying to run locally it has stopped working.自几个月前启动该项目以来,这一直有效,并继续在生产中工作,但是截至昨天尝试在本地运行时,它已停止工作。 When loading the page each of my helper inputs throws a null reference exception.
加载页面时,我的每个帮助器输入都会引发空引用异常。 I have tried using earlier versions of the project and it is throwing the same exception.
我曾尝试使用该项目的早期版本,但它引发了相同的异常。 I have been unable to find anyone with the same problem online and I am at a loss on how to fix this.
我一直无法在网上找到有同样问题的人,我不知道如何解决这个问题。 My only inclination at this point is to reinstall Visual Studio which I would prefer not to do.
在这一点上,我唯一的想法是重新安装我不想这样做的 Visual Studio。
EDIT: To clarify exactly what my issue is I have included the flow I am using below.编辑:为了明确我的问题是什么,我在下面包含了我正在使用的流程。 These are from a dummy project I created so my problem is not specific to any project.
这些来自我创建的一个虚拟项目,所以我的问题并不特定于任何项目。
I have a class here that I would like to input values as part of a form.我在这里有一门课,我想输入值作为表单的一部分。
public class Foo
{
public int TestProp1 { get; set; }
public string TestProp2 { get; set; }
}
I have many forms on one page so I must use a ViewModel to contain each of the multiple models.我在一个页面上有很多表单,所以我必须使用 ViewModel 来包含多个模型中的每一个。
public class IndexViewModel
{
public Foo Foo { get; set; }
public Bar Bar { get; set; }
}
Using Html helpers I create inputs to bind to the model on form submission.我使用 Html 助手创建输入以在表单提交时绑定到模型。
@model TestMvc2.Models.IndexViewModel
@{
ViewBag.Title = "Home Page";
}
<div class="row">
@using (Html.BeginForm("PostForm", "Home", FormMethod.Post))
{
<div class="form-group">
@Html.Label("Foo Test Prop 1")
@Html.TextBoxFor(c => c.Foo.TestProp1)
</div>
<div class="form-group">
@Html.Label("Foo Test Prop 2")
@Html.TextBoxFor(c => c.Foo.TestProp2)
</div>
}
</div>
When the page is loaded I get the following exceptions highlighted on the TextBoxFor extension:加载页面时,我在 TextBoxFor 扩展上突出显示以下异常:
Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in Microsoft.CSharp.dll抛出异常:Microsoft.CSharp.dll 中的“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”
Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in Microsoft.CSharp.dll抛出异常:Microsoft.CSharp.dll 中的“Microsoft.CSharp.RuntimeBinder.RuntimeBinderException”
Exception thrown: 'System.NullReferenceException' in System.Web.Mvc.dll Object reference not set to an instance of an object.抛出异常:System.Web.Mvc.dll 中的“System.NullReferenceException”对象引用未设置为对象的实例。
I am confused as to why this is the case because this is the flow I have used since beginning the project and it continues to work properly in production.我很困惑为什么会出现这种情况,因为这是我自项目开始以来一直使用的流程,并且它在生产中继续正常工作。 It is only when trying to debug locally that this is a problem
只有在尝试本地调试时才会出现问题
我有同样的问题,我修复了视觉工作室安装,问题消失了。
对我有用的是重启电脑后激活 Just My Code [1]: https ://i.stack.imgur.com/XjE1y.png
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.