2015-02-11 58 views
0
public ActionResult Countries(int id, int from=0) 

我想我的應用程序來改變從URL和控制器的參數,我想在我的查看按鈕,從+ = 10給出了,怎麼樣?發送參數來控制

<a class="k-button" href="@Url.Action("Countries", "Home",new {from+=10})">Next</a> 

似乎沒有工作,所以我想打電話給國家運作每次我都/ 20 /在我的網址30年代它必須每次+10按鈕點擊或-10無所謂,我只需要將我的URL或View中的值傳遞給Controller函數國家,怎麼樣?請幫忙,謝謝!

+0

你有任何變量,具有'from'的當前值? – haim770 2015-02-11 07:47:10

+0

您需要將一個變量傳遞給視圖以在url中表示您想要的新值 – 2015-02-11 07:47:26

+0

@ haim770變量?哪裏什麼?它改變了我的看法,但當我手動改變FROM ID爲10-20等,然後它打開一個新的視圖,並給我我想要的信息,所以我只需要改變URL參數我有/控制器/ ID /從我如何訪問FROM並給它一個值? – 2015-02-11 07:52:55

回答

1

您需要將偏移量和計數傳遞給服務器。

選項1:

在控制器動作只要定義網頁和每頁並用它來從DB過濾數據。

使用此選項您生成的URL會看起來像:

/主頁/國家頁= 1 &頁面大小= 10

選項2:

您可以定義有很多對象與頁#和每頁

public class DataSourceRequest 
    { 
    public int Page { get; set; } 

    public int PageSize { get; set; } 

    public IList<SortDescriptor> Sorts { get; set; } 

    public IList<IFilterDescriptor> Filters { get; set; } 

    public IList<GroupDescriptor> Groups { get; set; } 

    public IList<AggregateDescriptor> Aggregates { get; set; } 

    public DataSourceRequest() 
    { 
     this.Page = 1; 
     this.Aggregates = (IList<AggregateDescriptor>) new List<AggregateDescriptor>(); 
    } 
    } 

更新沿的更多信息:

OP說他想通過按鈕點擊增加一個值,建議你到 在頁面上定義一個JS全局變量,在你的鏈接中使用這個變量值,定義一個按鈕點擊事件, 10次​​。

更新2

你可以做到這樣說,這個工作對我來說:

Index.cshtml

@{ 
    ViewBag.Title = "title"; 
    Layout = null; 
} 
<script src="~/Scripts/JQuery/1.11.2/jquery-1.11.2.min.js"></script> 

<h2>title</h2> 
<a class="k-button">Next</a> 

<script language="javascript"> 
    var itemCount = 0; 

    $('.k-button').click(function() { 

     var model = { id: itemCount, from : itemCount }; 

     $.ajax({ 
      url: '@Url.Action("Countries", "Home")', 
      contentType: 'application/json; charset=utf-8', 
      type: 'POST', 
      dataType: 'html', 
      data: JSON.stringify(model) 
     }) 
     .success(function (result) { 
      alert(result); 
     }) 
     .error(function (xhr, status) { 
      alert(status); 
     }); 

     itemCount += 10; 
    }); 

</script> 

HomeController.cs

public class HomeController : Controller 
{ 
    // 
    // GET: /Test/ 

    public ActionResult Index() 
    { 
     return View(); 
    } 

    public ActionResult Countries(int id, int from = 0) 
    { 
     return Json(from); 
    } 
} 
+0

我有,'公共IEnumerable 匹配{get;組; } public int From {get;組; } public int To {get;組; } public int MatchCount {get;組; }'問題是我想更改URL參數/從,我不能從我的視圖..我如何使按鈕點擊從+ = 10?在視圖 – 2015-02-11 07:59:15

+0

@AlminIslamovic看到我的更新,並讓我知道如果我正確理解你的問題。 – SBirthare 2015-02-11 08:05:38

+0

是的,我明白你的意思,但我如何訪問JS的URL?有點新有此..:3 – 2015-02-11 08:20:55

0

好吧,這是我如何做它:`

@for (var i = 0; i < Model.MatchCount/20; i++) 
{ 
    if (i == Model.To) 
     { 
     <span>@(i + 1)</span> 
     } 
    else 
    { 
     <a id="Link" href="/#/Countries/@Model.Id/@(i *20)" data-pageindex="@i" 
      class="pager">@(i + 1)</a> 
    } 

}`

其中i * 20是我顯示的數量和Model.Id是當前頁面ID。它正在工作,所以是..