Almin Islamovic
Almin Islamovic

Reputation: 278

Send parameters from URL to Controller

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

I want my app to change FROM parameter in the URL and the controller, i want a button on my View that gives from+=10, how?

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

doesnt seem to work, so i want to call the Countries function every time i have /20 or /30 in my URL it must go +10 every time on button click or -10 doesn't matter, i just need to pass the value from my URL or View to the Controller function Countries, how? please help, thanks !

Upvotes: 0

Views: 876

Answers (2)

Almin Islamovic
Almin Islamovic

Reputation: 278

Ok, this is how i have done it: `

    @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> 
    }

}`

where i * 20 is the number i display and Model.Id is the current page ID. it is working so yeah..

Upvotes: 0

SBirthare
SBirthare

Reputation: 5137

You need to pass offset and count to the server.

Option 1:

Just define Page and PageSize in controller action and use it to filter the data from DB.

With this option your generated url will look like:

/Home/Countries?page=1&pagesize=10

Option 2:

You could define an object that has a lot more information along with Page# and 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>();
    }
  }

Update:

OP says he want to pass a value incrementing on button click, suggesting you to define a JS global variable on the page, use this variable value in your link, define a button click event in which you increment this variable by 10 every time.

Update 2:

You could achieve it this way, this works for me:

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

Upvotes: 1

Related Questions