Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax and MVC posting class object to controller
    text
    copied!<p>My Problem:</p> <p>First, I have a search form, where the user inputs criteria and the request is sent to the proper controllers index action. The user input is passed in as model type I create just for holding the data. I return a model into the view, then right before the view is rendered I get the View Model data using jquery, and I have a knockout.js/ajax post the 'data' to a jsonresult action, where search filtering happens then I update the view using the json data and knockouts data-bindings.</p> <p>My jsonresult action takes a string sortByText(works) for table sorting and a ViewModel. But when the ajax call occurs my jsonresult action does not recognize the values the data object passed.</p> <p>How can I pass 'SearchFormModel' correctly into the JSONresult action?</p> <p>AJAX</p> <p>'self.options.formModel' holds my data sent by the Index action; while debugging I see the that it has the data but it does not get posted to the jsonresult action in the next step, only the sortByText. Can ajax 'data:' only recognize strings and int?</p> <pre><code> $.ajax({ type: 'POST', url: Url, data: { sortByText: selectedText, formModel: self.options.formModel }, dataType: "json" })....etc </code></pre> <p>Controller</p> <pre><code> [HttpGet] public ActionResult Index(string sortByText, SearchFormModel formModel) { var model = new SearchViewModel(); model.FormModel = formModel; return View(model); } [HttpPost] public JsonResult GetData(string sortByText, SearchFormModel formModel) { //return jsonData } </code></pre>
 

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