-2

How can I remove a record from List in MVC 4 ASP.NET by click on Delete button Here I am not using any database I want to delete a record from list which I have define in controller. without any database remove a record from list using delete action

StudentController

public class StudentController : Controller
{
    //
    // GET: /Student/
    public ActionResult Index()
    {
        List<StudentVM> students = new List<StudentVM>();
        StudentVM obj1 = new StudentVM();
        obj1.Name = "Zeeshan";
        obj1.id = "1";
        obj1.Address = "Lahore";

        students.Add(obj1);


        StudentVM obj2 = new StudentVM();
        obj2.Name = "Zeshan";
        obj2.id = "2";
        obj2.Address = "Lahore";

        students.Add(obj2);
        return View(students);
    }

    public ActionResult Delete(string? i)
    {
        List<StudentVM> students = new List<StudentVM>();

        var st = students.Find(c => c.id = i);
        students.Remove(st);
        return View("Index");
    }
}

View

@model List<Activity2.Models.StudentVM>
@{
    ViewBag.Title = "Index";
}

<table border="1">
    <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Address</th>
    </tr>

    @foreach (var obj in Model)
    {
        <tr>
            <td>@obj.id</td>
            <td>@obj.Name</td>
            <td>@obj.Address</td>
            <td>@Html.ActionLink("Delete","Delete",new{i = obj.id}) </td>
        </tr>
    </table>
    }

Error

Error 1 The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'
Error 3 Cannot implicitly convert type 'string?' to 'string'

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109

2 Answers2

0

Firstly, as the error message is clearly saying you need to use string instead of string?:

public ActionResult Delete(string i)

And secondly you should be aware that, MVC is stateless because HTTP is. So this code won't delete the record as per your expectation. In order to make this code works, you need to use Session. Something like this:

if(Session["StudentVM"] == null)
{
    //Your Code
     Session["StudentVM"] = students;
}

And:

public ActionResult Delete(string i)
{
    List<StudentVM> students = Session["StudentVM"] as List<StudentVM>;
    var st = students.Find(c=>c.id=i);
    //The rest of your code...
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
0

You are trying to use nullable reference-types which is a feature of C# 8.0 (which hasn't been released yet). In order to fix your errors you'd have to change your string? i to string i.

To remove items from the list you'd have to create it outside of your Index() and make it static, so other Endpoints like Delete() are able to access the list. That way each time somebody accesses the Index of your Controller two new students are added to the List (probably not the behavior you'd want in the long run, i just copied your code on this one):

public class StudentController : Controller
{
    private static readonly List<StudentVM> _students = new List<StudentVM>();

    public ActionResult Index()
    {
        StudentVM obj1 = new StudentVM();
        obj1.Name = "Zeeshan";
        obj1.id = "1";
        obj1.Address = "Lahore";

        _students.Add(obj1);

        StudentVM obj2 = new StudentVM();
        obj2.Name = "Zeshan";
        obj2.id = "2";
        obj2.Address = "Lahore";

        _students.Add(obj2);

        return View(students);
    }

    public ActionResult Delete(string i)
    {
        var student = _students.FirstOrDefault(c => c.id == i);

        if(student == null){ /* Failed to find student */ }

        _students.Remove(student);
        return View("Index");
    }
}

Also there seems to be an Error in your View code. The </table> should be outside of the foreach.

Tobias Tengler
  • 6,848
  • 4
  • 20
  • 34
  • i have try your code this error come 1. ``Error 1 The name 'students' does not exist in the current context Error 2 Cannot convert lambda expression to delegate type 'System.Func' because some of the return types in the block are not implicitly convertible to the delegate return type Error 3 Cannot implicitly convert type 'string' to 'bool' `` – rajpoot rana Dec 08 '18 at 19:22
  • @rajpootrana the snippet i posted shouldn't have such an error. Where are you getting it? – Tobias Tengler Dec 08 '18 at 19:25
  • the error is because of id this error comes here ``var student = _students.FirstOrDefault(c => c.id = i);`` Error Can not implicitly convert type from 'string' to 'bool' – rajpoot rana Dec 08 '18 at 19:37
  • @rajpootrana Wupps, i copied it wrong from your code. It needs to be `var student = _students.FirstOrDefault(c => c.id == i);`. Now everything should work. If it does please mark this answer as the answer so others know the problem is solved. – Tobias Tengler Dec 08 '18 at 20:12
  • NullReference Exception was unhandle by user code ``@foreach (var obj in Model)`` – rajpoot rana Dec 08 '18 at 20:22
  • @rajpootrana this Exception has nothing todo with the question you've asked. Please mark my answer as the question as it likely solves the question you've asked. – Tobias Tengler Dec 09 '18 at 09:41