Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could send them as JSON request:</p> <pre><code>var categoryModel = { categoryID: 1, categoryName: "Beverage" }; var productModel = { productID: 1, productName: "Chai" }; $.ajax({ url: '@Url.Action("ModelTwo")', type: 'post', dataType: 'json', // It is important to set the content type // request header to application/json because // that's how the client will send the request contentType: 'application/json', data: JSON.stringify({ cat: categoryModel, prd: productModel }), cache: false, success: function (result) { alert(result); }, error: function (xhr, ajaxOptions, thrownError) { alert(thrownError); } }); </code></pre> <p>The <code>JSON.stringify</code> method that I am using in my example is natively built-in all modern browsers but if you need to support legacy browsers you could include the <a href="http://www.json.org/js.html" rel="noreferrer">json2.js</a> script to your page.</p> <p>This should correctly bind to the following action:</p> <pre><code>[HttpPost] public ActionResult ModelTwo(Category cat, Product prd) { return Json(new { message = "this took multiple model..." }); } </code></pre> <p>But I would recommend you defining a view model:</p> <pre><code>public class MyViewModel { public Category Cat { get; set; } public Product Prd { get; set; } } </code></pre> <p>and then have your controller action take this view model:</p> <pre><code>[HttpPost] public ActionResult ModelTwo(MyViewModel model) { return Json(new { message = "this took a single view model containing multiple models ..." }); } </code></pre> <p>and of course the client side code stays the same.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload