0

I have 4 model classes in my project. Every model class has an insert page. On every insert data page, I am getting the same error as System.NullReferenceException. This error is showing in each of the @Html.EditorFor attributes.

Suppose, I have 3 input attributes in my insert data page (.cshtml page), then every attribute shows the same error as System.NullReferenceException. I can not understand why is this happening? Please see the attached image. I can not debug this because errors are showing in .cshtml pages. Please help I am going crazy with this error.

When I instantiated the model class in my method and passed it to the view page it worked. But now I am curious to know if such then why the methods built by Entity Framework do not instantiate the model classes and pass to view page.

public ActionResult Create()
{
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "UserId,UserName,Password,Department,LocalLvl,Status")] UserModel userModel)
{
    if (ModelState.IsValid)
    {
        db.UserModels.Add(userModel);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(userModel);
}

The above method was created by the Entity Framework scaffolding process, why not instantiated the model class. And before, my project was running successfully then suddenly it starts to show the error? Please help.

enter image description here

enter image description here

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
user4221591
  • 2,084
  • 7
  • 34
  • 68
  • Are you passing in a instantiated model from your controller to the view? By convention at the top of the view you need to declare what type you are passing into the view. `@model YourNameSpace.Models.YourViewModel` – IceCode Sep 15 '21 at 06:32
  • Make sure that you return model to the View for your GET/POST method. For example: `return View(model);` – Yong Shun Sep 15 '21 at 06:35

1 Answers1

2

Expected that you had defined @model at the top of your page which the page is expected to receive that type of object.

@model YourProject.Models.LoginModel

And make sure that you have returned an instance of the model to pass to your view. Otherwise, the @model will be null.

public ActionResult Login()
{
    // Implementation
    var model = new LoginModel();

    return View(model);
}

[HttpPost]
public ActionResult Login(LoginModel model)
{
    // Implementation

    return View(model);
}

References

Strongly typed models and the @model directive

Yong Shun
  • 35,286
  • 4
  • 24
  • 46
  • 1
    Thank you for your answer it worked. But why not the default create method does not instantiate the model class? And how suddenly I started getting error. Please can you suggest/reply that too. – user4221591 Sep 15 '21 at 09:04
  • 1
    Hi, glad that my answer helps you. Because the view is expected to receive `@model`, without return model to view via Controller, the @model will not have value, hence it is `null`. You may read the article that I shared in **References**. The tutorial will help you understand and familiar with ASP.NET MVC. – Yong Shun Sep 15 '21 at 09:11