-2

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.

Merv
  • 37
  • 8
  • Why do you provide a parameter (`notes` or `asdf`) that you don't use? Are you actually posting the real code here? It doesn't make sense. – topsail Nov 09 '22 at 00:45
  • No this is just an example of the problem. I removed the unnecessary code to highlight the issue I'm having. Code or no code this issue exists and I'm clueless as to why. But i agree, this doesn't make sense... – Merv Nov 09 '22 at 00:49
  • But the actual name in the question is `notes` – stuartd Nov 09 '22 at 00:51
  • Edited question for more clarity, this is the code how I'm using it now. – Merv Nov 09 '22 at 00:53
  • Can you share what the calling request that fails looks like? – stuartd Nov 09 '22 at 00:53
  • what is the meaning of those comments `// notes == null` and `// model is not null` - what is that all about? – topsail Nov 09 '22 at 00:55
  • @topsail I'm submitting data from a form to an object / model. If the object is called "note" it will give me a null reference error. If i call the object anything other than note, it will work and give me no error. – Merv Nov 09 '22 at 00:57
  • I would also like to see this calling request. Your language is not clear here. Are you *sure* you know where the null reference exception is actually being raised, and why? It should have nothing to do with choosing one or the other of these names for your parameter. – topsail Nov 09 '22 at 01:00
  • @topsail I'm sorry, I'll update the question with everything related. Give me a minute. – Merv Nov 09 '22 at 01:02
  • Doesn't really make sense but maybe some confusion in the model binding because of a field called Note in this model. I would just chalk it up as one of life's mysteries and carry on ... so, just not using that name. – topsail Nov 09 '22 at 01:21
  • @Merv - This seems like an interesting question and the duplicate probably isn't a great fit here. Can you provide us with a [mcve]? That means detailing the steps for us to replicate the issue you're seeing from a brand new project. – Enigmativity Nov 09 '22 at 01:26
  • @Merv - I've re-opened the question. Can you make sure you are clear what a [mcve] is and that you post one? – Enigmativity Nov 09 '22 at 01:53
  • 1
    @Enigmativity I have updated the question with detailed information on how to recreate it. If you have any more questions let me know. – Merv Nov 09 '22 at 03:22

2 Answers2

1

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.

Crunchers3
  • 180
  • 5
  • Using author or title also gives a null value. I thought it would be case sensitive though since the names in the form all start with upper case letters. Anyway, mystery solved thanks. – Merv Nov 09 '22 at 16:49
  • @Merv -- C# is case sensitive, HTML is not, so by extension C# cannot reliably determine case when getting fields from an HTTP action. Glad its solved :) – Crunchers3 Nov 09 '22 at 16:53
0

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.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466