title: How to pass Model and json object to controller Action
body: <p>I looked for a long time for a solution to this problem but I cant find anything yet.  I have defined a strong type as the model of a controller/view that I'm using; the problem is that the model references other complex types, so I need to partially update sections of that model.  For example, in one tab of the page I get partially another view that brings a grid with some selections; another part brings another section; so after the user selects their options and submits, I need to send all those small pieces to the post operation.</p>
  
  <p>I already created the JSON object that I need and sent it the controller.  At the action, I succesfully get the model, but the JSON does not come to the action. So how can i pass that JSON to the view as an additional parameter to the controller action?</p>
  
  <p>I also already checked the request with fiddler and the JSON is being sent.  Does the object get passed in a special collection?</p>
  
  <hr>
  
  <p>Sure. The view expects or is strongly typed to a Model, in this case is a "Provider" entity. This entity has primitive types and complex types. SInce the entity is still in creation process i need to pass partial objects to complete the entity when the post create action is reached. 
  Heres part of the model:</p>
  
  <pre><code>public class Provider2 : IProvider
  {
      public int Id { get; set; }
      public bool IsApproved { get; set; }
      public string RejectionNotes { get; set; }
      public string Name { get; set; }
      public string Phone { get; set; }
      public string Fax { get; set; }
      public string Email { get; set; }
      public int OfficeAddressId { get; set; }
      public virtual Address Address { get; set; }
      public virtual ICollection&lt;Chain&gt; ProviderChain { get; set; }
      public virtual ICollection&lt;ProviderContact&gt; ProviderContacts { get; set; }
      public virtual ICollection&lt;ExaminationPrice&gt; ExaminationPrices { get; set; }
  }
  </code></pre>
  
  <p>THere is no problem with the simple types such as an integer, string and so on.
  As you can see, there are collections and complex types, these types are retrieved from partial views and an that moment are separate entities, so i need at submit time to wrap them perhaps in a json or another object such as JSON and get it at the moment that the create action is reached:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Create(Provider provider, Another object to be passed???)
  {
      if (ModelState.IsValid)
      {
          _repository.Save(provider);
          return RedirectToAction("Index");
      }
      return View(provider);
  }
  </code></pre>
  
  <p>The primitive types and the addres properties are mapped at the model, so they are no problem, but the collections are not mapped, so thats the reason for me to trying to pass an aditional object such a json to wrap all the parts from plain HTML to javascript objects because theyre are not mapped directly to the model.</p>
  
  <p>So, by this javascript im sending the data i need to the view and its sent, but apparently the action does not receives the json object.</p>
  
  <pre><code>  $(function () {
          $("#ButtonSave").click(function () {
              var prices = getPrices();
  
              if (prices != null) {
                  $.ajax({
                      url: '/Provider/Create',
                      type: 'POST',
                      dataType: 'json',
                      data: prices,
                      contentType: 'application/json; charset=utf-8',
                      success: function (data) {
                          alert('Success');
                      }
                  });
              }
          });
      });
  
      function getPrices() {
          var elements = '[';        
          var rows = $("#selectedElementsGrid tr.selectable").each(function () {
              var idValue = '{price : { "Id":' + $(this).find(".id").val() + ',';
              var nameValue = ' "Name":"' + $(this).find(".name").text() + '",';
              var priceValue = ' "Price":"' + $(this).find(".price").val() + '"} },';
              elements = elements + idValue + nameValue + priceValue;
          });
          elements = elements.substring(0, elements.length - 1) + ']';
          return JSON.stringify(elements);
      }
  </code></pre>
  
  <p>Thanks in advance!</p>