0

I have cshtml view page with paginations

here view of that page

enter image description here

Once I click any of below number (which is 1 to 10) I should be able to pass that number to POST method of that form

this is the relevant cshtml code snippet to that pagination

   @if (Model.Pager.EndPage > 1)
                    {
                        <ul class="pagination">
                        @for (var page = Model.Pager.StartPage; page <= Model.Pager.EndPage; page++)
                        {
                                <li class="@(page == Model.Pager.CurrentPage ? "active" : "")">
                                   <input type="submit" value=@page class="btn btn-default" ViewBag.pagenumber=@page/>
                                </li>
                        }
                       </ul>
                    }

then I try to pass that value like this

@using (Html.BeginForm("method_name", "controller", new { pagenumber = ViewBag.pagenumber} , FormMethod.Post))
{

but pagenumber getting null each time, in this way


EDIT:

    [HttpGet]
    public ActionResult method_name(int? page,string Product_ID)
    {
     ........
    }

    [HttpPost]
    [ValidateInput(false)]
    public ActionResult method_name(AddNewProduct product, string pagenumber)
    {
    .....
    return RedirectToAction("method_name", "controller", new { page = pagenumber});
    }
kez
  • 2,273
  • 9
  • 64
  • 123
  • You should also share your controller code. Maybe the problem is there. – Deblaton Jean-Philippe Jan 12 '16 at 10:34
  • Are you trying to put ViewBag value to the input or trying to post the value to controller using ViewBag from input? – Anil Panwar Jan 12 '16 at 10:35
  • Why are you assigning ViewBag in your view. Try this `@using (Html.BeginForm("method_name", "controller", new { pagenumber = Model.Pager.CurrentPage } , FormMethod.Post))` – adiga Jan 12 '16 at 10:38
  • @adiga actually I want to assign the page of that I'm clicking , not current page – kez Jan 12 '16 at 10:50
  • @AnilPanwar I'm trying to post the value to controller using ViewBag from input , but this value is dynamic , it can be 1-10 value, that's why I'm trying to assign moment I'm clicking – kez Jan 12 '16 at 10:53
  • @kez yes, my bad. If page number is the only parameter you need, you can try this http://stackoverflow.com/a/1714311/3082296 – adiga Jan 12 '16 at 10:56

3 Answers3

0

in ViewBag value becomes null when redirection occurs. try to use TempData

   <input type="submit" value=@page class="btn btn-default" TempData["pagenumber"]=@page/> 

    @using (Html.BeginForm("method_name", "controller", new { pagenumber = TempData["pagenumber"]} , FormMethod.Post))
Deneden
  • 1
  • 2
0

Assign a name tag to each input field. In that way you could retrieve the values in the controller using a FormCollection and don't need the new { pagenumber = ViewBag.pagenumber}:

[HttpPost]
[ValidateInput(false)]
public ActionResult method_name(FormCollection collection){
  var inputValue = collection.GetValue("name").AttemptedValue;
  return RedirectToAction("method_name", "controller", inputValue);
}
Sjors Hijgenaar
  • 1,232
  • 1
  • 16
  • 30
0

You can do it like

Add class pagerBtn in the pager buttons

<input type="submit" value=@page class="btn btn-default pagerBtn"/>

Make a hidden input field in the form

     @using (Html.BeginForm("method_name", "controller" , FormMethod.Post))
{
      //Make sure the action parameter same as name of input field
    <input type="hidden" name="pagenumber" id="hiddenInput" />
}

Write jquery to get page number

$(document).ready(function(){
$(".pagerBtn").on("click",function(e){
   var pageClicked = e.target.value;//Get clicked button page number from attribute value.
 $("#hiddenInput").val(pageClicked);//insert clicked value in hidden filed input with name same as controller parameter taking this value.

});
});
Anil Panwar
  • 2,592
  • 1
  • 11
  • 24
  • how to pass this value to post method ? – kez Jan 12 '16 at 11:00
  • Updated the answer please review again, as i have inserted the hidden input filed in the form, thus this value will be post in the form it self and action parameter will catch this page value as the name of the hidden input and action parameter is same, hope you got an idea by now. – Anil Panwar Jan 12 '16 at 11:07
  • why this ` – kez Jan 12 '16 at 11:14
  • This will keep the value of clicked page number, if your pager button is not inside the form. – Anil Panwar Jan 12 '16 at 11:21
  • in this approach , can it grab model values also , instead parameters ? – kez Jan 12 '16 at 11:22
  • Yes, you will get other things as it is, if you are using strong type and if not then the input name should be same as the name of the parameters.I can see your code and it will work fine, go on. – Anil Panwar Jan 12 '16 at 11:27
  • Still `pagenumber` is null , once I debug this using firebut I can see following error `TypeError: e.target.attr is not a function` – kez Jan 12 '16 at 11:37
  • Use e.target.value instead e.target.attr("value"), updated code as well. – Anil Panwar Jan 12 '16 at 11:42