简体   繁体   English

将模型和其他参数从View传递到Controller Razor MVC3

[英]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. 我有一个具有多个字段的表单的视图,这是我的View模型。 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: 我有以下javascript代码,将其作为OnClick动作绑定到页面链接:

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. 该绑定代码在每个页面的循环中,因此“ i”对应于为其创建链接的页面。

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. 按照您的方式,这些值已过帐到ShowCustomers操作方法中,但对返回的值不做任何事情。

If ShowCustomers() is returning either JSON or HTML, then you need to display these information: 如果ShowCustomers()返回JSON或HTML,则需要显示以下信息:

$.post('@Url.Action("ShowCustomers","Home")', PostData,
      function(data)
      {
        $('#results') = data;
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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