Returns an object from class "Depot" which contains a list with objects from "Gegenstand" class.
public ActionResult Index()
{
Depot depotObject = new Depot();
Gegenstand gegenstandObject = new Gegenstand { Beschreibung = "Door" };
depotObject.depotItems.Add(gegenstandObject);
return View(depotObject);
}
The index.cshtml which displays the objects from the list. Now I want to post the object "Gegenstand" to the controller (Comment area)
@model MvcApplication2.Models.Depot
<table>
@foreach(MvcApplication2.Models.Gegenstand gegenstand in Model.depotItems)
{
<tr>
<td>
@using (Html.BeginForm("Details", "Home", FormMethod.Post))
{
// Want to post "Gegenstand" object to controller
<input type="submit" value="click" />
}
</td>
</tr>
}
</table>
This is the ActionResult for "Details"
[HttpPost]
public ActionResult Details(Gegenstand gegenstandObject)
{
return View(gegenstandObject);
}
POSTrequest at all to show something? You need a get request instead with an id of the object and fetch it in your details view.depotItemsin the form (you haven't show the model so hard to say), but why have a separate form foreachdepotItemsas opposed to posting back the collection ofdepotItems? And what is the purpose of the POST method if all you do is return the view (your not saving anything)?input type="submit"tag.Details()method suggests you are displaying details of adepotItem. I suspect you really just want an action link that redirects to display details of thedepotItem, in which case a form is not required (its a GET, not a POST)