How could I fetch a parameter from this MVC architecture?












0















I have this HTML page in an ASP .NET project, and I need to generate a pagination for a table of elements. Problem is, in the class architecture that's already in place, the only thing I can have for my model is an IEnumerable of a view model. The ONLY thing I would need is to fetch an integer value from either my controller or the view it returns. From that integer, that would represent the number of buttons needed to generate the pagination, I would create it, see HTML.



My controller generates the list of items and returns in the model it by doing a SQL Request that selects a certain amount of item from a certain offset, depending on my URL's parameters.



Here's the HTML, and what the code-behind in the controller would look like:



@model  IEnumerable<ItemIndexViewModel>

<h2>@UiText.PageTitles.ITEM_LIST</h2>
<hr />
<div class="col-md-12">
<div class="col-md-9">
<table class="table" id="client-index">
<thead>
<tr>
<th class="green-table-head-1">
@Html.DisplayNameFor(model => model.Name)
</th>
<th class="green-table-head-1">
@Html.DisplayNameFor(model => model.Id)
</th>
</tr>
</thead>
<tbody>
@foreach (ItemViewModel item in Model)
{
@*Here, I have my table of items being generated*@
}
</tbody>
</table>
<div id="pagination">
<ul>
@for (int i = 0; i < [I need my int right here]; i++)
{
@*I will generate buttons here*@
}
</ul>
</div>
</div>
</div>




public virtual ActionResult Index()
{
int ownerId = _httpContext.GetUserOwnerId();
int amountPerPage = 0;
int pageIndex = 0;

Int32.TryParse(Request.QueryString["amountPerPage"], out amountPerPage);
Int32.TryParse(Request.QueryString["pageIndex"], out pageIndex);

if (amountPerPage <= 0)
{
amountPerPage = 10;
}
if (pageIndex <= 0)
{
pageIndex = 1;
}

List<Item> items = _itemRepository.GetByPage(pageIndex, amountPerPage).ToList();

// Make view models from the list of items
List<ItemIndexViewModel> itemIndexViewModels = Mapper.Map<List<ItemIndexViewModel>>(items);

// Create the buttons for the HTML
int totalAmount = _itemRepository.Count();
int totalPages = (Int32)Math.Ceiling(Decimal.Divide(totalAmount, amountPerPage));

// Set update the navigation trace
SetTraceRoot(MVC.Item.Index(), MVC.Item.ActionNames.Index);

return View(itemIndexViewModels.OrderBy(x => x.Name));
}


What would be a good way of generating a pagination? I'm looking for flexibility because this procedure will be implemented for more than one page and for more than one class of items. I've already tried a couple of things to no avail, like using a class to contain my list of view models and an integer for the number of pages needed to store them all.










share|improve this question

























  • Since you can't change the page model, use the ViewBag to pass the pagination object.

    – Jasen
    Nov 27 '18 at 18:18











  • This is exactly what we need. Please post this as an answer so I may approve it.

    – mgrandmont
    Nov 27 '18 at 18:25
















0















I have this HTML page in an ASP .NET project, and I need to generate a pagination for a table of elements. Problem is, in the class architecture that's already in place, the only thing I can have for my model is an IEnumerable of a view model. The ONLY thing I would need is to fetch an integer value from either my controller or the view it returns. From that integer, that would represent the number of buttons needed to generate the pagination, I would create it, see HTML.



My controller generates the list of items and returns in the model it by doing a SQL Request that selects a certain amount of item from a certain offset, depending on my URL's parameters.



Here's the HTML, and what the code-behind in the controller would look like:



@model  IEnumerable<ItemIndexViewModel>

<h2>@UiText.PageTitles.ITEM_LIST</h2>
<hr />
<div class="col-md-12">
<div class="col-md-9">
<table class="table" id="client-index">
<thead>
<tr>
<th class="green-table-head-1">
@Html.DisplayNameFor(model => model.Name)
</th>
<th class="green-table-head-1">
@Html.DisplayNameFor(model => model.Id)
</th>
</tr>
</thead>
<tbody>
@foreach (ItemViewModel item in Model)
{
@*Here, I have my table of items being generated*@
}
</tbody>
</table>
<div id="pagination">
<ul>
@for (int i = 0; i < [I need my int right here]; i++)
{
@*I will generate buttons here*@
}
</ul>
</div>
</div>
</div>




public virtual ActionResult Index()
{
int ownerId = _httpContext.GetUserOwnerId();
int amountPerPage = 0;
int pageIndex = 0;

Int32.TryParse(Request.QueryString["amountPerPage"], out amountPerPage);
Int32.TryParse(Request.QueryString["pageIndex"], out pageIndex);

if (amountPerPage <= 0)
{
amountPerPage = 10;
}
if (pageIndex <= 0)
{
pageIndex = 1;
}

List<Item> items = _itemRepository.GetByPage(pageIndex, amountPerPage).ToList();

// Make view models from the list of items
List<ItemIndexViewModel> itemIndexViewModels = Mapper.Map<List<ItemIndexViewModel>>(items);

// Create the buttons for the HTML
int totalAmount = _itemRepository.Count();
int totalPages = (Int32)Math.Ceiling(Decimal.Divide(totalAmount, amountPerPage));

// Set update the navigation trace
SetTraceRoot(MVC.Item.Index(), MVC.Item.ActionNames.Index);

return View(itemIndexViewModels.OrderBy(x => x.Name));
}


What would be a good way of generating a pagination? I'm looking for flexibility because this procedure will be implemented for more than one page and for more than one class of items. I've already tried a couple of things to no avail, like using a class to contain my list of view models and an integer for the number of pages needed to store them all.










share|improve this question

























  • Since you can't change the page model, use the ViewBag to pass the pagination object.

    – Jasen
    Nov 27 '18 at 18:18











  • This is exactly what we need. Please post this as an answer so I may approve it.

    – mgrandmont
    Nov 27 '18 at 18:25














0












0








0








I have this HTML page in an ASP .NET project, and I need to generate a pagination for a table of elements. Problem is, in the class architecture that's already in place, the only thing I can have for my model is an IEnumerable of a view model. The ONLY thing I would need is to fetch an integer value from either my controller or the view it returns. From that integer, that would represent the number of buttons needed to generate the pagination, I would create it, see HTML.



My controller generates the list of items and returns in the model it by doing a SQL Request that selects a certain amount of item from a certain offset, depending on my URL's parameters.



Here's the HTML, and what the code-behind in the controller would look like:



@model  IEnumerable<ItemIndexViewModel>

<h2>@UiText.PageTitles.ITEM_LIST</h2>
<hr />
<div class="col-md-12">
<div class="col-md-9">
<table class="table" id="client-index">
<thead>
<tr>
<th class="green-table-head-1">
@Html.DisplayNameFor(model => model.Name)
</th>
<th class="green-table-head-1">
@Html.DisplayNameFor(model => model.Id)
</th>
</tr>
</thead>
<tbody>
@foreach (ItemViewModel item in Model)
{
@*Here, I have my table of items being generated*@
}
</tbody>
</table>
<div id="pagination">
<ul>
@for (int i = 0; i < [I need my int right here]; i++)
{
@*I will generate buttons here*@
}
</ul>
</div>
</div>
</div>




public virtual ActionResult Index()
{
int ownerId = _httpContext.GetUserOwnerId();
int amountPerPage = 0;
int pageIndex = 0;

Int32.TryParse(Request.QueryString["amountPerPage"], out amountPerPage);
Int32.TryParse(Request.QueryString["pageIndex"], out pageIndex);

if (amountPerPage <= 0)
{
amountPerPage = 10;
}
if (pageIndex <= 0)
{
pageIndex = 1;
}

List<Item> items = _itemRepository.GetByPage(pageIndex, amountPerPage).ToList();

// Make view models from the list of items
List<ItemIndexViewModel> itemIndexViewModels = Mapper.Map<List<ItemIndexViewModel>>(items);

// Create the buttons for the HTML
int totalAmount = _itemRepository.Count();
int totalPages = (Int32)Math.Ceiling(Decimal.Divide(totalAmount, amountPerPage));

// Set update the navigation trace
SetTraceRoot(MVC.Item.Index(), MVC.Item.ActionNames.Index);

return View(itemIndexViewModels.OrderBy(x => x.Name));
}


What would be a good way of generating a pagination? I'm looking for flexibility because this procedure will be implemented for more than one page and for more than one class of items. I've already tried a couple of things to no avail, like using a class to contain my list of view models and an integer for the number of pages needed to store them all.










share|improve this question
















I have this HTML page in an ASP .NET project, and I need to generate a pagination for a table of elements. Problem is, in the class architecture that's already in place, the only thing I can have for my model is an IEnumerable of a view model. The ONLY thing I would need is to fetch an integer value from either my controller or the view it returns. From that integer, that would represent the number of buttons needed to generate the pagination, I would create it, see HTML.



My controller generates the list of items and returns in the model it by doing a SQL Request that selects a certain amount of item from a certain offset, depending on my URL's parameters.



Here's the HTML, and what the code-behind in the controller would look like:



@model  IEnumerable<ItemIndexViewModel>

<h2>@UiText.PageTitles.ITEM_LIST</h2>
<hr />
<div class="col-md-12">
<div class="col-md-9">
<table class="table" id="client-index">
<thead>
<tr>
<th class="green-table-head-1">
@Html.DisplayNameFor(model => model.Name)
</th>
<th class="green-table-head-1">
@Html.DisplayNameFor(model => model.Id)
</th>
</tr>
</thead>
<tbody>
@foreach (ItemViewModel item in Model)
{
@*Here, I have my table of items being generated*@
}
</tbody>
</table>
<div id="pagination">
<ul>
@for (int i = 0; i < [I need my int right here]; i++)
{
@*I will generate buttons here*@
}
</ul>
</div>
</div>
</div>




public virtual ActionResult Index()
{
int ownerId = _httpContext.GetUserOwnerId();
int amountPerPage = 0;
int pageIndex = 0;

Int32.TryParse(Request.QueryString["amountPerPage"], out amountPerPage);
Int32.TryParse(Request.QueryString["pageIndex"], out pageIndex);

if (amountPerPage <= 0)
{
amountPerPage = 10;
}
if (pageIndex <= 0)
{
pageIndex = 1;
}

List<Item> items = _itemRepository.GetByPage(pageIndex, amountPerPage).ToList();

// Make view models from the list of items
List<ItemIndexViewModel> itemIndexViewModels = Mapper.Map<List<ItemIndexViewModel>>(items);

// Create the buttons for the HTML
int totalAmount = _itemRepository.Count();
int totalPages = (Int32)Math.Ceiling(Decimal.Divide(totalAmount, amountPerPage));

// Set update the navigation trace
SetTraceRoot(MVC.Item.Index(), MVC.Item.ActionNames.Index);

return View(itemIndexViewModels.OrderBy(x => x.Name));
}


What would be a good way of generating a pagination? I'm looking for flexibility because this procedure will be implemented for more than one page and for more than one class of items. I've already tried a couple of things to no avail, like using a class to contain my list of view models and an integer for the number of pages needed to store them all.







c# html asp.net asp.net-mvc-4 pagination






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 6:25









Lauren Van Sloun

1,02921019




1,02921019










asked Nov 27 '18 at 17:34









mgrandmontmgrandmont

154




154













  • Since you can't change the page model, use the ViewBag to pass the pagination object.

    – Jasen
    Nov 27 '18 at 18:18











  • This is exactly what we need. Please post this as an answer so I may approve it.

    – mgrandmont
    Nov 27 '18 at 18:25



















  • Since you can't change the page model, use the ViewBag to pass the pagination object.

    – Jasen
    Nov 27 '18 at 18:18











  • This is exactly what we need. Please post this as an answer so I may approve it.

    – mgrandmont
    Nov 27 '18 at 18:25

















Since you can't change the page model, use the ViewBag to pass the pagination object.

– Jasen
Nov 27 '18 at 18:18





Since you can't change the page model, use the ViewBag to pass the pagination object.

– Jasen
Nov 27 '18 at 18:18













This is exactly what we need. Please post this as an answer so I may approve it.

– mgrandmont
Nov 27 '18 at 18:25





This is exactly what we need. Please post this as an answer so I may approve it.

– mgrandmont
Nov 27 '18 at 18:25












1 Answer
1






active

oldest

votes


















0














You can use ViewBag or ViewData instance in controller action to provide paging value before returning view:



ViewBag.TotalPages = totalPages;


And pass its value to the for loop to generate pagination in view side:



@for (int i = 0; i < ViewBag.TotalPages; i++)
{
@* generate paging buttons *@
}


Usually ViewBag properties don't require type cast for simple types (numeric & string values), but ensure that the value is assigned to avoid return null.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53505156%2fhow-could-i-fetch-a-parameter-from-this-mvc-architecture%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can use ViewBag or ViewData instance in controller action to provide paging value before returning view:



    ViewBag.TotalPages = totalPages;


    And pass its value to the for loop to generate pagination in view side:



    @for (int i = 0; i < ViewBag.TotalPages; i++)
    {
    @* generate paging buttons *@
    }


    Usually ViewBag properties don't require type cast for simple types (numeric & string values), but ensure that the value is assigned to avoid return null.






    share|improve this answer




























      0














      You can use ViewBag or ViewData instance in controller action to provide paging value before returning view:



      ViewBag.TotalPages = totalPages;


      And pass its value to the for loop to generate pagination in view side:



      @for (int i = 0; i < ViewBag.TotalPages; i++)
      {
      @* generate paging buttons *@
      }


      Usually ViewBag properties don't require type cast for simple types (numeric & string values), but ensure that the value is assigned to avoid return null.






      share|improve this answer


























        0












        0








        0







        You can use ViewBag or ViewData instance in controller action to provide paging value before returning view:



        ViewBag.TotalPages = totalPages;


        And pass its value to the for loop to generate pagination in view side:



        @for (int i = 0; i < ViewBag.TotalPages; i++)
        {
        @* generate paging buttons *@
        }


        Usually ViewBag properties don't require type cast for simple types (numeric & string values), but ensure that the value is assigned to avoid return null.






        share|improve this answer













        You can use ViewBag or ViewData instance in controller action to provide paging value before returning view:



        ViewBag.TotalPages = totalPages;


        And pass its value to the for loop to generate pagination in view side:



        @for (int i = 0; i < ViewBag.TotalPages; i++)
        {
        @* generate paging buttons *@
        }


        Usually ViewBag properties don't require type cast for simple types (numeric & string values), but ensure that the value is assigned to avoid return null.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 '18 at 1:29









        Tetsuya YamamotoTetsuya Yamamoto

        16.6k42241




        16.6k42241
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53505156%2fhow-could-i-fetch-a-parameter-from-this-mvc-architecture%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Lallio

            Futebolista

            Jornalista