将参数从URL发送到Controller

时间:2015-02-11 07:43:22

标签: c# asp.net asp.net-mvc

public ActionResult Countries(int id, int from=0)

我希望我的应用程序更改URL和控制器中的FROM参数,我希望我的View上的按钮从+ = 10开始,如何?

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

似乎无法正常工作,所以我想每次在我的网址中使用/ 20或/ 30时都要调用“国家/地区”功能每次按下按钮时它必须为+10或者-10并不重要,我只是需要将值从我的URL或View传递给Controller函数国家,如何?请帮助,谢谢!

2 个答案:

答案 0 :(得分:1)

您需要将偏移量和计数传递给服务器。

选项1:

只需在控制器操作中定义Page和PageSize,并使用它来过滤来自数据库的数据。

使用此选项,您生成的网址将如下所示:

/主页/国家页= 1&安培;页大小= 10

选项2:

您可以定义一个具有更多信息的对象以及Page#和PageSize

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

答案 1 :(得分: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。它正在工作所以是啊..

相关问题