Click here to Skip to main content
14,733,542 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am calling Delete controller method from ajax on button click but when i click the button it gives error like "Object reference not set to an instance of an object."

I have write a code for the ajax and calling delete method previously the javascript code get called on every page load rather than on button click but then i add e.preventDefault(); The issue is now it is giving error rather then moving to the controller delete method.

My Javascript code
    function DeleteCall(id) {

          // $(document.getElementById("sbmtInput").click(function(e){}
           $('btnDelete').click(function (e) {

       type: "POST",
       url: '@Url.Action("Delete", "Student_Experience")',
       data: JSON.stringify({ id: id }), //use id here
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       success: function () {
               // alert("Data has been deleted.");
       error: function () {
               alert("Error while deleting data");

my Button code
<input type="submit" id="btnDelete" onclick="DeleteCall(@item.ID)" value="Delete" class="btn btn-sm btn-danger" />

My Controller code
       public ActionResult Delete(int? id)
           if (id == null)
               return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
           Student_Experience student_Experience = db.Student_Experience.Find(id);
           if (student_Experience == null)
               return HttpNotFound();
          // Student_Experience student_Experience = db.Student_Experience.Find(id);
           return RedirectToAction("Create","Students");
          // return View(student_Experience);

What I have tried:

As shown above i have tried to write the code but i am getting error on clicking delete button "
Object reference not set to an instance of an object.
" also it get call on page load rather than on click
Updated 31-Oct-19 20:05pm
F-ES Sitecore 25-Oct-19 5:30am
What line throws the error?
Malikdanish 25-Oct-19 5:37am
Line 86: Line 87:
Line 88: @foreach (var item in Model)
Line 89: {
Line 90:
Andy Lanng 25-Oct-19 5:54am
There is no "foreach" in your post
F-ES Sitecore 25-Oct-19 5:57am
That code isn't in the code you have pasted in your question. Check that "Model" isn't null, if it is null work out why.
ZurdoDev 25-Oct-19 7:04am
Something is null. This is such a simple bug to fix and only you can do it because we can't run your code. Figure out what is null and then fix it. Easy.
Kornfeld Eliyahu Peter 27-Oct-19 10:36am
It is impossible to see in your code, but Model is null (the controller didn't created it?)...
You should use simple examples and the debugger...

1 solution

Your error shows that you didn't describe your model in a view page. That's why you get an error on for-each. So first, try to run code after mentioning model class in view.

Next, Make correction in your JavaScript code:
$('btnDelete').click(function (e){
You have used id to submit button so must put # before btnDelete like this:

Hope This will help you.
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900