Reputation: 8678
I'm trying to get a Id of item from view so that I can delete it from db. Here is my model
public class ModuleVM
{
public long Id { get; set; }
public string ModuleId { get; set; }
public string TypeName { get; set; }
[DataType(DataType.DateTime)]
public DateTime DateEntered { get; set; }
}
public class IndexModule
{
public List<ModuleVM> Modules { get; set; }
}
Here is method from controller that shows all the items from table
public ActionResult ModuleList()
{
var modulesList = new IndexModule();
var modules = (from module in _db.Modules
orderby module.DateEntered
select new ModuleVM
{
Id = module.Id,
ModuleId = module.ModuleId,
TypeName = module.ModuleType.TypeName,
DateEntered = module.DateEntered
});
modulesList.Modules = modules.ToList();
return View(modulesList);
}
And now the view
@using BootstrapSupport
@model AdminPortal.Areas.Hardware.Models.IndexModule
@{
ViewBag.Title = "Index";
Layout = "~/Views/shared/_BootstrapLayout.basic.cshtml";
}
<p>
@Html.ActionLink("Create New", "CreateModule", null, new {@class = "btn"})
</p>
<table class="table table-striped">
<caption></caption>
<thead>
<tr>
<th>ID</th>
<th>Module Id</th>
<th>Module type</th>
<th>Date Entered</th>
</tr>
</thead>
@foreach (var entry in Model.Modules)
{
<tr>
<td>@entry.Id</td>
<td>@entry.ModuleId</td>
<td>@entry.TypeName</td>
<td>@entry.DateEntered</td>
<td>
<div class="btn-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
Action<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li>@Html.ActionLink("Details", "Create")</li>
@if (Request.IsAuthenticated && HttpContext.Current.User.IsInRole("Admin"))
{
<li class="divider"></li>
<li>@Html.ActionLink("Edit", "Edit")</li>
<li>@Html.ActionLink("Delete", "DeleteModule","Module", new {Id = entry.Id})</li>//error probably here
}
</ul>
</div>
</td>
</tr>
}
</table>
and now the delete method
public ActionResult DeleteModule(long Id)
{
// Mapper.CreateMap<Sorama.DataModel.SIS.ModuleStock.Module, ModuleVM>();
var moduletypeId = Convert.ToInt64(_db.ModuleTypes.Find(moduleVM.TypeName));
var module = new Sorama.DataModel.SIS.ModuleStock.Module
{
Id = moduleVM.Id,
ModuleId = moduleVM.ModuleId,
ModuleTypeId = moduletypeId,
DateEntered = moduleVM.DateEntered
};
//_db.Modules.Remove(module);//also this shows error, don't know why
Information("Your widget was deleted");
return RedirectToAction("ModuleList", "Module", new { area = "Hardware" });
}
Edit: as per answer I tried to change my view On Pressing delete action I get this message in browser.
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int64' for method 'System.Web.Mvc.ActionResult DeleteModule(Int64)' in 'AdminPortal.Areas.Hardware.Controllers.ModuleController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
How can I pass the Id from my foreach
statement from view to controller, when delete is pressed?
Upvotes: 0
Views: 2860
Reputation: 4266
Your not getting anything in there because your not passing anything to the controller. it is getting a default object.
There is no form on there to pass back data in a POST. Normally for a delete or edit on a grid you would add the ID of the record onto the route and use that to edit/delete your object.
So
public ActionResult DeleteModule(ModuleVM moduleVM)
becomes
public ActionResult DeleteModule(long Id)
and your action link becomes
<li>@Html.ActionLink("Delete", "DeleteModule","Module", new { Id = entry.Id })</li>
Then change your code to use the Id and delete the record.
Upvotes: 3
Reputation: 62260
entry.Id should assign to routesValues instead of htmlAttributes
Here is the fix -
@Html.ActionLink("Delete", "DeleteModule", routeValues: new { Id = entry.Id })
Upvotes: 0