Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. I use the Html Helpers to create input boxes such as TextBoxFor and TextAreaFor in the forms as shown below.

@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.

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.

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.

@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:

Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in Microsoft.CSharp.dll

Exception thrown: 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' in Microsoft.CSharp.dll

Exception thrown: 'System.NullReferenceException' in System.Web.Mvc.dll Object reference not set to an instance of an object.

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

like image 931
ilang898 Avatar asked Mar 26 '20 21:03

ilang898


1 Answers

I had the same problema and I repared visual studio instalation and the problem desapear.

like image 83
Lucas Luis Baruffi Avatar answered Nov 04 '22 06:11

Lucas Luis Baruffi