6

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

4
  • Are you the only person working on this project? There might be a change in the models that isn't reviewed thoroughly. Commented Mar 26, 2020 at 21:28
  • I am the only person working on the project and I am facing this issue on a dummy project as well so it is not project specific. I have made an edit that will hopefully clarify my problem. Commented Mar 27, 2020 at 23:19
  • I can't find the one I'm looking for-- will you include your controller action that creates the viewmodel and displays the view? thanks. Commented Mar 27, 2020 at 23:31
  • So I did some more tests and it seems that it is a problem with the debugger. If I run without debugging then it runs perfectly fine. I am not sure where to begin with solving this now. It is thrown when passing over the Html Helper methods so my inclination is that there may be a broken dll somewhere but I am unfamiliar with dlls. Commented Mar 28, 2020 at 1:14

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me too, but it had the effect of removing all the settings I'd applied and the arrangement of windows. Very annoying.
2

What worked for me was activating Just My Code after restarting the PC:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.