0

I need to send two objects to my controller using bindings but I have no idea how to do it. I just know how to send one object not multiple objects.

This is an example of how I'm doing it with a singular object.

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<Inventario.Models.Report>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Create</h2>

<script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>

<% using (Html.BeginForm()) { %>
    <%: Html.ValidationSummary(true) %>
    <fieldset>
        <legend>Report</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Client, "Client_1") %>
        </div>
        <div class="editor-field">
            <%: Html.DropDownList("Client", String.Empty) %>
            <%: Html.ValidationMessageFor(model => model.Client) %>
        </div>



        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
<% } %>

<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>

</asp:Content>

Then, It is received by the controller this way:

[HttpPost]
public ActionResult Create(Report report)
{
    //Some code here
}

And what I need is to get two objects. IDK maybe something like this:

[HttpPost]
public ActionResult Create(Report report, AnotherObject ao)
{
    //Some code here
}

I'm a beginer on this. Any guide you can give me will help me a lot. Thanks by advance.

tereško
  • 58,060
  • 25
  • 98
  • 150
Jamaldo
  • 83
  • 1
  • 7

1 Answers1

0

You have to create a ViewModel like this:

public class MyViewModel
{
 public Report report {get;set;} 
 pulic AnotherObject ao {get;set;}
}

From you Get Action pass them:

public ActionResult Create()
{
   MyViewModel model = new MyViewModel();
   model.report = new Report();
   model.ao = new AnotherObject();

   return View(model);
}

In your view set Model to MyViewModel

and post it in action:

[HttpPost]
public ActionResult Create(MyViewModel myModel)
{
    //Some code here
}
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • Hi @Ehsan, I followed your suggestion and my model is now like this: public class MyViewModel { public Report report {get;set;} public IEnumerable Details {get;set;} } my question now is: How can I add several ReportDetail model objects from my view to be sent in the form using Html helpers (<%: Html.anyhelper%>) – Jamaldo May 19 '14 at 07:14
  • and this http://stackoverflow.com/questions/19964553/mvc-form-not-able-to-post-list-of-objects – Ehsan Sajjad May 19 '14 at 07:21
  • and also this one:http://stackoverflow.com/questions/17468279/posting-a-model-containing-a-list-in-mvc-asp-net – Ehsan Sajjad May 19 '14 at 07:21
  • Hey @Ehsan, those links were so helpful. But now I have my last question: my MyViewModel has the object Report as a property called "report". This Report object has their own properties too. What would be the name in the view page? I mean if were using a property like this in my model: String city. In the view I should put I imagine it would be something like this ?? Thanks. – Jamaldo May 19 '14 at 21:03