简体   繁体   中英

Passing Model and extra parameters from View to Controller Razor MVC3

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.

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;
      });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM