0

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;
    }
user3820903
  • 23
  • 1
  • 5

1 Answers1

0

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.

abdulbasit
  • 1,856
  • 15
  • 17
  • I changed it with "id=item" and with a "ApplicationModel id" param but it still send a new instance of ApplicationModel – user3820903 Jul 10 '14 at 10:22
  • Ok, I will do it with the id but isn't it better to communicate with object? With object, I would not have to research it all time in the database... – user3820903 Jul 10 '14 at 10:55