Home > front end >  Object null reference when it's named "note", why?
Object null reference when it's named "note", why?

Time:11-10

Why does this give me a object null reference error:

[HttpPost]
public ActionResult WriteNote(NotesModel note) //notes == null
{
    SendToDB(note.Author, note.Title, note.Note);
    return View(); //BREAKPOINT
}

And this does not:

[HttpPost]
public ActionResult WriteNote(NotesModel model) //model is not null
{
    SendToDB(model.Author, model.Title, model.Note);
    return View(); //BREAKPOINT
}

I've spend about 2 hours trying to figure out what is wrong and i just found this out by accident but have no idea why this works. No matter what name i choose for the object anything will do but note...

Can anyone explain?

-----EDIT:

TO RECREATE THIS ISSUE DO THE FOLLOWING

Create a new ASP.NET (Framework 4.8.1) MVC project. Create a Model called "NotesModel.cs" with the following content:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

    namespace WebsiteProject.Models
    {
        public class NotesModel
        {
            [Display(Name = "Author")]
            public string Author { get; set; }
    
            [Display(Name = "Title")]
            public string Title { get; set; }
    
            [Display(Name = "Note")]
            public string Note { get; set; }
        }
}

Next create a controller called "NotesController.cs" with the following content:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebsiteProject.Models;
using System.Windows;

namespace WebsiteProject.Controllers
{
    public class NotesController : Controller
    {
        public ActionResult WriteNote()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult WriteNote(NotesModel note)
        {
            if (note == null) MessageBox.Show("IF YOU ARE SEEING THIS THEN note == NULL");
            return View();
        }
    }
}

Next go to the NotesController.cs file and right click on public ActionResult WriteNote() and select "Add View", choose MVC 5 View.

For Template choose: Create

For Model Class Choose the NotesModel

Go to the newly created view and launch the webapp, fill in the three textboxes and submit.

Now you should see a textbox telling you that note == NULL, this is obviously wrong, it should have received data from the form.

Now if you go to the NotesController.cs file and replace

public ActionResult WriteNote(NotesModel note)

With this

public ActionResult WriteNote(NotesModel somethingelse)

(Also change note to somethingelse in the if statement)

It will no longer be null and actually receive the data from the form. you can check this by inserting a breakpoint at the if statement. Also the messagebox should not be popping up now.

I hope this is detailed enough to recreate the issue.

CodePudding user response:

The parameter name matches a form field name, so it is getting confused at whether you want the bag or just the field. Try author or title to confirm.

CodePudding user response:

This is expected behavior. The name of the parameter is used by ASP.NET to figure out what form elements map to that parameter. Although you didn't show your markup, it appears you have an element (or elements) using the name model but not note.

Rename your parameter or rename the HTML elements.

  • Related