0

I have a view in which I have a form with several fields, which is my View model. I would like to make a list of links (for a pager) with which when I click a certain page it would send the input data in the form + a page. I have the following javascript code, which I bind to the page links as an OnClick action:

function SearchCriteria() {
    this.OrderNumber = "";
    this.CustomerNumber = "";
    this.FirstName = "";
    this.LastName = "";
    this.Login = "";
    this.Company = "";
    this.Country = "";

}

function sendModel(page) {

    var myModel = new SearchCriteria();

    var PostData = JSON.stringify(myModel);
    $.post('@Url.Action("ShowCustomers","Home")', PostData);

}

The problem is that when I click one of the page numbers - nothing happens. As if the script isn't called.

Code to bind the function to the links:

<a class="@(i == ViewBag.CurrentPage ? "current" : "")" onclick="sendModel(@i)" href="#">@(innerContent ?? i.ToString())</a> 

This binding code is in a loop for each page, so "i" corresponds to the page for which the link is created.

4
  • Can you post the code you are using to generate the links, or the HTML produced for the links and the jQuery code you are using to bind the click event to the links? The problem might be in the binding.
    – Andy T
    Aug 19, 2013 at 15:44
  • @QuetiM.Porta, I have updated the question with the information you wanted :)
    – Martin K
    Aug 19, 2013 at 15:56
  • Is the sendModel() method in a .cshtml file or .js file?
    – Andy T
    Aug 19, 2013 at 15:58
  • It's defined in the same file as where it is being set as an onClick action.
    – Martin K
    Aug 19, 2013 at 15:59

1 Answer 1

0

The way you have it, the values are being POSTed to the ShowCustomers action method, but are not doing anything with what the returned values.

If ShowCustomers() is returning either JSON or HTML, then you need to display these information:

$.post('@Url.Action("ShowCustomers","Home")', PostData,
      function(data)
      {
        $('#results') = data;
      });
2
  • Well, the action method returns just a view, without any data, what am I supposed to do then? (The table displaying is handled by the server)
    – Martin K
    Aug 19, 2013 at 16:11
  • If it is returning only the results, then simply replace the section of the page that currently shows the results with the returned results (basically the code that I have). Otherwise, if it is returning the entire page, then I would recommend that you do not use the ajax approach and instead use javascript to create a link that has all the parameters and simply redirect the user to that page.
    – Andy T
    Aug 19, 2013 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.