2

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);
    }
5
  • Why do you do a POST request at all to show something? You need a get request instead with an id of the object and fetch it in your details view. Commented Dec 18, 2014 at 21:39
  • 1
    You just need to generate the controls for each property of depotItems in the form (you haven't show the model so hard to say), but why have a separate form foreach depotItems as opposed to posting back the collection of depotItems? And what is the purpose of the POST method if all you do is return the view (your not saving anything)? Commented Dec 18, 2014 at 22:02
  • codeproject.com/Articles/758458/… has some View-to-Controller help. Commented Dec 18, 2014 at 22:08
  • Start off by thinking of posting HTML input tags, not objects. Which tags do you expect to post? I see no tags except the input type="submit" tag. Commented Dec 18, 2014 at 22:12
  • A Details() method suggests you are displaying details of a depotItem. I suspect you really just want an action link that redirects to display details of the depotItem, in which case a form is not required (its a GET, not a POST) Commented Dec 18, 2014 at 23:00

1 Answer 1

2

You need to build a Gegenstand object in your view.

You can achieve this two ways.

Use the @Html.EditorFor within MVC in your form and let the framework take care of the model binding.

For instance: @Html.EditorFor(m => m.YourProperty);

Or by building the object up and pass a serialized object back up to your Controller. You can use JavaScript for this and POST it back to the controller via an AJAX call. For instance.

<script>
    function CreateGegenstandObject() {
         var obj = {};
         obj.property = "Your property";  // This should reflect the property in your C# class
         obj.property2 = "Another property"; // Another property that should be reflected

         return obj;
    }

    function sendGegenstandObjectToController() {
          var gegenstandObject = CreateGegenstandObject(); 
          $.ajax({
             url: '@Url.Action("Details")',
             type: "POST",
             data: { gegenstandObject: gegenstandObject.serialize() },
             success: function() { alert('success'); }
          });
    }

</script>

You would have to invoke the sendGegenstandObjectToController function once the form has been submitted.

Sign up to request clarification or add additional context in comments.

6 Comments

@Donkeyy No. You could do it ASP.Net MVC style by passing the object to the controller on submit
When I have multiple objects, how can I post only one which i selected?
@Donkeyy look at Stephen Muecke comment above. Generate the controls for each property.
@TWhite @Donkeyy - I've added an example using @Html.EditorFor
@Donkeyy Darren has a good answer. He shows you the Razor MVC way with the \@HTML or with a AJAX call the JS way. Either will work. Also, keep in mind the questions Vsevolod and Stephen raise about the POST versus GET call.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.