0

My ajax:

$.ajax({
                            url: '/Extensions/Sample',
                            type: 'GET',
                            async: false,
                            dataType: 'text',
                            processData: false,
                            //contentType: 'application/json; charset=utf-8',
                            data: "extension=" + JSON.stringify(newextension),
                            success: function (data) {
                                alert("Success");
                            }
                        }).error(function (jqXHR, textStatus, errorThrown) {
                            alert(jqXHR.status);
                            alert(jqXHR.responseText);
                            alert(errorThrown);
                        });

My Controller:

 public ActionResult Sample(Extension extension)
    {           
        return PartialView(extension);
    }

My Model:

 public class Extension
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Number { get; set; }
}

My Json :

 newextension = [{
                        'Name': 'User1',
                        'Number': '101'
                    },
                    {
                        'Name': 'User2',
                        'Number': '102'
                    },
                    {
                        'Name': 'User3',
                        'Number': '103'
                    }];

The error is Object reference not set to an instance of an object. where is the mistake and how can i resolve this issue . Any help ..

John Saunders
  • 160,644
  • 26
  • 247
  • 397
DS kumar
  • 347
  • 2
  • 4
  • 11
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Oct 13 '13 at 02:28

2 Answers2

0

In your model, remove the Id property in class Extension or add the Id in your JSON, as the returned JSON and the parameter doesn't match and thus your object is not getting initialized.

UPDATE : The JSON you're sending is a List, so i guess your need to use List<Extension> extension as parameter

AthibaN
  • 2,087
  • 1
  • 15
  • 22
  • Even if i remove the id from my model then also the same error was displayed .. @AthibaN – DS kumar Oct 10 '13 at 10:31
  • you trying to GET or POST ? – AthibaN Oct 10 '13 at 10:33
  • i am calling ajax from a separate .js file .. will that make any problem ... i dont think so – DS kumar Oct 10 '13 at 10:34
  • i want to display the view with the passed data .. what diference will it make if i use GET or POST – DS kumar Oct 10 '13 at 10:35
  • Check my answer to your other question http://stackoverflow.com/a/19292019/2121389 and more over, the JSON you're sending is a List, so i guess your need to use `List extension` as parameter – AthibaN Oct 10 '13 at 10:38
0

It will be helpful if you put the stack trace. One problem I see here is that the parameter to Sample is an object of type Extension, while the json has an array of those objects. My guess is that the extension parameter will by null in this case which leads to the NullReferenceException.

One possible this is to have the signature look like this:

public ActionResult Sample(Extension[] extension)
    {           
        return PartialView(extension);
    }

But then I'm not sure what will the PartialView when it receives an array.

Alon Catz
  • 2,417
  • 1
  • 19
  • 23
  • Parameter is of type Extension and dat which i send will be json .. how to convert the json object into the paramter type i.e Extension type – DS kumar Oct 10 '13 at 10:51