Note that there are some explanatory texts on larger screens.

plurals
  1. POHTML.BeginForm does not call controller asp.net MVC 4
    primarykey
    data
    text
    <p>I have a partial view. It's Model is a Book object and Customer object</p> <p>I've tried to use a input type=submit button with the following:</p> <pre><code>@using (Html.BeginForm("GetBooks", "Home", FormMethod.Post, new { id = "formId" })) { } </code></pre> <p>I can get the user entered values with <code>var values = $('#formId').serialize();</code></p> <p>But when clicking on the button, the controller does not get called.</p> <p>So, I decided to use an jquery ajax() method and it appears to work fine, by calling the controller and passing in the model. However, it's not sending the updated model (updated with the user entry) to the controller. </p> <p>example of ajax :</p> <pre><code>$.ajax({ url: "/Home/GetBooks", data: JSON.stringify({ model: @Html.Raw(Json.Encode(Model)) }) , type: 'POST', contentType: 'application/json', beforeSend:function(){ $("#loading").show(); }, success: function(data) { append(data) }, error: function (e, textStatus, jqXHR) { $("#loading").hide(); alert(e.statusText); }, complete:function(){ $("#loading").hide(); } }); </code></pre> <p>I need to re-assign the user entered values to the Book Model before I call the Ajax method, but I am unsure how to do this. I am passing both Book and Customer Model back to the controller as seen in </p> <pre><code>JSON.stringify({model: @Html.Raw(Json.Encode(Model))}) </code></pre> <p>controller:</p> <pre><code>[HttpPost] public ActionResult GetBooks(ModelObjects model) { } </code></pre> <p>Model:</p> <pre><code>public class ModelObjects { public MVC4App.Models.Customer Customer{ get; set; } public MVC4App.Models.Book Book{ get; set; } } public class Customer { public string FirstName{ get; set; } public string LastName{ get; set; } } public class Book { public string Name{ get; set; } } </code></pre> <p>Model reference in the View:</p> <pre><code>@model MVC4App.Models.ModelObjects </code></pre> <p>Thanks in advance!</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.
 

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