I have a partial view. It's Model is a Book object and Customer object
I've tried to use a input type=submit button with the following:
@using (Html.BeginForm("GetBooks", "Home", FormMethod.Post, new { id = "formId" }))
{
}
I can get the user entered values with var values = $('#formId').serialize();
But when clicking on the button, the controller does not get called.
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.
example of ajax :
$.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();
}
});
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
JSON.stringify({model: @Html.Raw(Json.Encode(Model))})
controller:
[HttpPost]
public ActionResult GetBooks(ModelObjects model)
{
}
Model:
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; }
}
Model reference in the View:
@model MVC4App.Models.ModelObjects
Thanks in advance!