-2
 Controller: 

          public ActionResult Delete(int id)
                    {
     Student _std = new Student();
                    var abc = _dbcon.StudentList.Where(c => c.Roll_ID.Equals(id)).SingleOrDefault();
                    _dbcon.StudentList.Remove(abc);
                    _dbcon.SaveChanges();
                    return View("Test");

                }
    This is my view and the error comes at foreach loop
    View:

                   @foreach (StudentViewModel _EachStd in @Model.StudentList)
                 {
                            <tr>
                                <td>  @_EachStd.Roll_ID</td>
                                <td> @_EachStd.Name</td>
                                <td> @_EachStd.Maths</td>
                                <td> @_EachStd.Urdu</td>
                                <td> @_EachStd.English</td>
                                <td>@_EachStd.ObtainNumber</td>
                                <td>@_EachStd.Percentage</td>
                                <td> @_EachStd.Grade</td>
                              <td>@Html.ActionLink("Edit", "Edit", "Home", new {id= @_EachStd.Roll_ID },null)</td>
                                <td>@Html.ActionLink("Delete", "Delete", "Home", new { id = @_EachStd.Roll_ID }, null)</td>
                            </tr>
                }
            </tbody>
                </table>

I got Null Error exception, but after refresh i got the record delete. But why the error occur I dont get It.Whenever i run this code my Edit controller is working correctly but my Delete controller is not working correctly, and error is occur like there is "Null erroe exception"

  • It's not clear at all what the problem is or what you're asking. Please provide clear and readable code along with a description of the problem and what you're attempting to achieve. – David Feb 28 '19 at 12:48
  • Now i clearly provide everything please guide me as soon as possible – Faraz Siddique Feb 28 '19 at 13:19
  • Already voted to close for a different reason, but now it also sounds like this is a duplicate of another oft-cited question: https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – David Feb 28 '19 at 13:20

1 Answers1

0

error is occur like there is "Null erroe exception"

You mean a NullReferenceException? That would mean something is null that you're trying to use as though it has a value.

This is my view and the error comes at foreach loop

So then something on this line is null?:

@foreach (StudentViewModel _EachStd in @Model.StudentList)

The only thing you're trying to dereference on that line is Model. So it follows that Model is null. Are you passing a model to your view?

return View("Test");

No, you are not. You need to pass a model to your view in order to use that model within your view.

As an aside, returning a view from this delete operation probably isn't the way to go in the first place. Consider that the user would now be on the URL /Home/Delete and viewing it as a page. This would quickly get confusing for both the user and your code.

Instead of returning a view, especially one without a model, redirect the user to the action which builds the model and displays the view. Which in your code could be something as simple as:

return RedirectToAction("Test");
David
  • 208,112
  • 36
  • 198
  • 279
  • I did everything but still have same issue, the problem is that, when i click on my delete button, it goes to my code and give me error of null exception. When i reload my project and run again, the record is deleted which i want to delete. – Faraz Siddique Mar 01 '19 at 07:28
  • @FarazSiddique: Can you add your updated code to the question? – David Mar 01 '19 at 11:37