I started an ASP.NET MVC 4 Project. I have a problem to communicate between View and Controller with my model. I know how to send a List to the view but I want to retrieve this model from the view to send it to the controller.
In my controller :
public ActionResult Index()
{
List<ApplicationModel> listAppli = ApplicationModel.GetListApplications();
return View(listAppli);
}
public ActionResult Delete(ApplicationModel app)
{
ApplicationModel.DeleteApplication(app);
return RedirectToAction("Index");
}
And my view is a list. I want to retrieve the model corresponding to the row when I Delete/Details/Edit.
@model IEnumerable < Activate.Models.ApplicationModel>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Code)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
@Html.ActionLink("Edit", "Edit", new { id=item }) |
@Html.ActionLink("Details", "Details", new { id=item }) |
@Html.ActionLink("Delete", "Delete", new{ id=item } )
</td>
</tr>
}
In my ActionLink, i just want to send the object 'item', but the item send on the event is a new item which doesn't correspond to the row.
Is their an error in my code or I have to use another way ? Like another tools to create links between View and Model which allows to send Object in parameters ?
Thank you.
My ApplicationModel is like :
public ApplicationModel(ApplicationModel app)
{
Id = app.Id;
Code = app.Code;
Name = app.Name;
}
You can not post a model as a parameter. Instead you should try to send id to your controller then get the model from the db.