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'