How can i use pagination for showing data dynamically in div in asp .net using ajax or jquery?
To be honest, it's hard to help you - you should be more specific, but maybe you are looking for sth like this:
jQuery pagination plugin
and
demo here
You haven't really asked a real question, but maybe this will help: https://github.com/TroyGoode/PagedList
You can create pagination on your div using bootstrap and jquery.
Controller
public ActionResult Index()
{
// Tab Data
ThumbnailViewModel model = new ThumbnailViewModel();
model.ThumbnailModelList = new List<ThumbnailModel>();
// Test Details Data
List<ThumbnailDetails> _detaisllist = new List<ThumbnailDetails>();
int count = 10;
for (int i = 1; i <= count; i++)
{
ThumbnailDetails obj = new ThumbnailDetails();
obj.Details1 = "Details- Main" + i;
obj.Details2 = "Details- Main-Sub" + i;
_detaisllist.Add(obj);
}
// batch your List data for tab view i want batch by 2 you can set your value
var listOfBatches = _detaisllist.Batch(2);
int tabNo = 1;
foreach (var batchItem in listOfBatches)
{
// Generating tab
ThumbnailModel obj = new ThumbnailModel();
obj.ThumbnailLabel = "Lebel" + tabNo;
obj.ThumbnailTabId = "tab" + tabNo;
obj.ThumbnailTabNo = tabNo;
obj.Thumbnail_Aria_Controls = "tab" + tabNo;
obj.Thumbnail_Href = "#tab" + tabNo;
// batch details
obj.ThumbnailDetailsList = new List<ThumbnailDetails>();
foreach (var item in batchItem)
{
ThumbnailDetails detailsObj = new ThumbnailDetails();
detailsObj = item;
obj.ThumbnailDetailsList.Add(detailsObj);
}
model.ThumbnailModelList.Add(obj);
tabNo++;
}
// Getting first tab data
var first = model.ThumbnailModelList.FirstOrDefault();
// Getting first tab data
var last = model.ThumbnailModelList.LastOrDefault();
foreach (var item in model.ThumbnailModelList)
{
if (item.ThumbnailTabNo == first.ThumbnailTabNo)
{
item.Thumbnail_ItemPosition = "first";
}
if (item.ThumbnailTabNo == last.ThumbnailTabNo)
{
item.Thumbnail_ItemPosition = "last";
}
}
return View(model);
}
View:
#model ThumbnailPagination.Models.ThumbnailViewModel
#{
ViewBag.Title = "Home Page";
}
<div class="container">
<div class="col-xs-10 col-md-6 col-xs-offset-1 col-md-offset-3">
<div class="row">
<nav aria-label="...">
<ul class="pager" role="tablist">
<li class="previous" onclick="goTo(1);"><span aria-hidden="true">←</span> Previous</li>
#{
foreach (var item in Model.ThumbnailModelList)
{
if (item.Thumbnail_ItemPosition == "first")
{
<li class="active" id="#item.Thumbnail_ItemPosition">
<a aria-controls="#item.Thumbnail_Aria_Controls" data-toggle="tab" href="#item.Thumbnail_Href" role="tab">#item.ThumbnailTabNo</a>
</li>
}
else if (item.Thumbnail_ItemPosition == "last")
{
<li id="#item.Thumbnail_ItemPosition">
<a aria-controls="#item.Thumbnail_Aria_Controls" data-toggle="tab" href="#item.Thumbnail_Href" role="tab">#item.ThumbnailTabNo</a>
</li>
}
else
{
<li>
<a aria-controls="#item.Thumbnail_Aria_Controls" data-toggle="tab" href="#item.Thumbnail_Href" role="tab">#item.ThumbnailTabNo</a>
</li>
}
}
}
<li class="next" onclick="goTo(2);">Next <span aria-hidden="true">→</span></li>
</ul>
</nav>
</div>
<!-- Tab panes -->
<div class="tab-content">
#{
foreach (var item in Model.ThumbnailModelList)
{
if (item.Thumbnail_ItemPosition == "first")
{
<div class="tab-pane active" id="#item.ThumbnailTabId" role="tabpanel">
#{
foreach (var detailsitem in item.ThumbnailDetailsList)
{
<div class="col-sm-6">
<div class="thumbnail">
<img alt="..." src="http://placehold.it/240x150">
<div class="caption">
<h3>#detailsitem.Details1</h3>
<p>
#detailsitem.Details2
</p>
<p>
<a class="btn btn-primary" href="#" role="button">
Read more
...
</a>
</p>
</div>
</div>
</div>
}
}
</div>
}
else
{
<div class="tab-pane" id="#item.ThumbnailTabId" role="tabpanel">
#{
foreach (var detailsitem in item.ThumbnailDetailsList)
{
<div class="col-sm-6">
<div class="thumbnail">
<img alt="..." src="http://placehold.it/240x150">
<div class="caption">
<h3>#detailsitem.Details1</h3>
<p>
#detailsitem.Details2
</p>
<p>
<a class="btn btn-primary" href="#" role="button">
Read more
...
</a>
</p>
</div>
</div>
</div>
}
}
</div>
}
}
}
</div>
</div>
</div>
<style>
.pager .active a {
background-color: #337AB7;
color: #FFF;
border: 0px;
}
</style>
<script>
function goTo(number) {
$('ul.pager li:eq(' + number + ') a').tab('show');
upgradePreNext(number);
}
function upgradePreNext(number) {
if (number > 1) {
$('ul.pager li:eq(0)').attr("onclick", "goTo(" + (number - 1) + ")");
$('ul.pager li:eq(0)').attr("class", "previous");
} else {
$('ul.pager li:eq(0)').attr("class", "disabled");
}
if (number < 5) {
$('ul.pager li:eq(6)').attr("onclick", "goTo(" + (number + 1) + ")");
$('ul.pager li:eq(6)').attr("class", "next");
} else {
$('ul.pager li:eq(6)').attr("class", "disabled");
}
}
$(document).ready(function () {
$('li a').on('click', function (e) {
goTo((e.target.innerHTML) - 0);
});
});
</script>
Out put will be:
You can also download the sample code.
Related
I am building a mini online shopping CMS project for collage.
I finished the project and now I want to add a feature where I have limited quantity of products.
When the quantity is equal to 0 then product is shown as 'out of stock' and my 'Add to cart' button becomes disabled.
I have added a 'quantity' property to my product class and now I need to change something in the Index view of the product.
I tried few options and it does not seem to work.
Here is the code for the Index.cshtml page.
#model IEnumerable
<Product>
#{ ViewData["Title"] = "Products"; }
<h1 class="display-4 pb-5">All Products</h1>
<div class="row">
#foreach (var item in Model)
{
<div class="col-4">
<div class="ajaxbg d-none">
<img src="~/Images/ajax_loader.gif" />
<p class="lead alert alert-success text-center d-none">
The product has been added!
</p>
</div>
<img src="~/Media/Products/#item.Image" class="img-fluid" alt="" />
<h4>#item.Name</h4>
<div>
#Html.Raw(item.Description)
</div>
<p>
#item.Price.ToString("C2")
</p>
<p>
<a asp-controller="Cart" asp-action="Add" asp-route-id="#item.Id" data-id="#item.Id" id="addToCartButton" class="btn btn-primary addToCart">Add to cart</a>
</p>
#if (item.Quantity == 0) { }
</div>
}
#if (ViewBag.TotalPages > 1) {
<div class="d-flex w-100 justify-content-center">
<pagination page-count="#ViewBag.TotalPages" page-target="/products" page-number="#ViewBag.PageNumber" page-range="ViewBag.PageRange"></pagination>
</div>
}
</div>
#section Scripts {
<script>
function DisableBtn() {
document.getElementById("addToCartBUtton").disabled = true;
}
$(function() {
$("a.addToCart").click(function(e) {
e.preventDefault();
let ajaxDiv = $(this).parent().parent().find("div.ajaxbg");
ajaxDiv.removeClass("d-none");
let id = $(this).data("id");
$.get('/cart/add/' + id, {}, function(data) {
$("div.smallcart").html(data);
ajaxDiv.find("img").addClass("d-none");
ajaxDiv.find("p").removeClass("d-none");
setTimeout(() => {
ajaxDiv.animate({
opacity: 0
}, function() {
$(this).addClass("d-none").fadeTo(.1, 1);
$(this).find("img").removeClass("d-none");
$(this).find("p").addClass("d-none");
});
});
});
});
});
</script>
}
Try this
#If(Model!=null)
{
#foreach (var item in Model)
{
..........
<p>
#if (item.Quantity == 0) {
<a disabled class="btn btn-primary addToCart">Add to cart</a>
}
else
{
<a asp-controller="Cart" asp-action="Add" asp-route-id="#item.Id" data-id="#item.Id" id="addToCartButton" class="btn btn-primary addToCart">Add to cart</a>
}
</p>
}
}
I have a main view which has two tabs. The first tab is the default tab and should display the first partial view and the second tab should display the second partial view. While I am getting that to work properly, I am not able to get the tab highlighted for the particular selected tab.
My controller.
public ActionResult Contact(int condition=0)
{
if(condition==0)
ViewBag.Message = "Tab1";
else
ViewBag.Message = "Tab2";
ViewBag.Condition = condition;
return View();
}
My View
#{
ViewBag.Title = "Contact";
}
<h2>#ViewBag.Title.</h2>
<div>
<ul class="nav nav-tabs">
<li>#Html.ActionLink("Tab1","Contact", new { condition=0})</li>
<li>#Html.ActionLink("Tab2", "Contact", new { condition = 1 })</li>
</ul>
<div class="tab-content">
#if (ViewBag.Condition == 0)
{
<div class="tab-pane active" id="Tab1">
#Html.Partial("_Partial1")
</div>
}
else
{
<div class="tab-pane active" id="Tab2">
#Html.Partial("_Partial2")
</div>
}
</div>
</div>
EDIT 1: As suggested by an answer does not solve the problem.
#{
ViewBag.Title = "Contact";
}
<h2>#ViewBag.Title.</h2>
<div>
<ul class="nav nav-tabs">
<li>#Html.ActionLink("Tab1","Contact", new { condition=0}, new { #class = ViewBag.Condition == 0 ? "active" : "" })</li>
<li>#Html.ActionLink("Tab2", "Contact", new { condition = 1 }, new { #class = ViewBag.Condition == 1 ? "active" : "" })</li>
</ul>
<div class="tab-content">
#if (ViewBag.Condition == 0)
{
<div class="tab-pane active" id="Tab1">
#Html.Partial("_Partial1")
</div>
}
else
{
<div class="tab-pane active" id="Tab2">
#Html.Partial("_Partial2")
</div>
}
</div>
</div>
EDIT 2:
<div id="tabs">
<ul class="nav nav-tabs">
<li class="nav active">#Html.ActionLink("Tab1", "Contact", new { condition = 0 }, new { #id = "Tab1", data_toggle = "tab" })</li>
<li class="nav">#Html.ActionLink("Tab2", "Contact", new { condition = 1 }, new { #id = "Tab2", data_toggle = "tab" })</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade in active" id="Tab1">
#Html.Partial("_Partial1")
</div>
<div class="tab-pane fade" id="Tab2">
#Html.Partial("_Partial2")
</div>
</div>
</div>
I have changed the code to be based on this fiddle
Does not make much difference.
I am sure there are better ways of doing it but this solution works for me. I just check the Viewbag everytime and set the class of li active based on the check. This way I end up setting the right li element even though the screen has a call to the action method on tab change.
<div>
<ul class="nav nav-tabs">
#if (ViewBag.Condition == 0)
{
<li class="nav active">#Html.ActionLink("Tab1", "Contact", new { condition = 0 }, new { #id = "Tab1" })</li>
<li class="nav">
#Html.ActionLink("Tab2", "Contact", new { condition = 1 }, new{#id = "Tab2"})
</li>
}
else if (ViewBag.Condition == 1)
{
<li class="nav">#Html.ActionLink("Tab1", "Contact", new { condition = 0 }, new { #id = "Tab1"})</li>
<li class="nav active">#Html.ActionLink("Tab2", "Contact", new { condition = 1 }, new { #id = "Tab2"})</li>
}
</ul>
<div class="tab-content">
#if (ViewBag.Condition == 0)
{
<div class="tab-pane fade in active" id="Tab1">
#Html.Partial("_Partial1")
</div>
}
else if (ViewBag.Condition == 1)
{
<div class="tab-pane fade in active" id="Tab2">
#Html.Partial("_Partial2")
</div>
}
</div>
<div>
<ul class="nav nav-tabs">
<li>#Html.ActionLink("Tab1", "Contactme", new { condition = 0 })</li>
<li>#Html.ActionLink("Tab2", "Contactme", new { condition = 1 })</li>
</ul>
<div class="tab-content">
#if (ViewBag.Condition == 0)
{
<div class="tab-pane active" id="Tab1">
#Html.Partial("_Partial1")
</div>
}
else
{
<div class="tab-pane active" id="Tab2">
#Html.Partial("_Partial2")
</div>
}
</div>
</div>
I have a problem with calling my function that show the books for same author, for example I have a these authors :
Author Name : Jeremy McPeak; Joe Fawcett; Nicholas C. Zakas;
When I click on "Jeremy McPeak" and "Joe Fawcett" the new page opened well and show all the books for those authors .
But when click on this author "Nicholas C. Zakas" and since he has a DOT in his name , I got exception
Object reference not set to an instance of an object.
This problem only when I click on any author that have a DOT In his name ..
also here is my function :
public ActionResult Author(string AuthorName, int? page)
{
if (AuthorName != null)
{
ViewBag.AuthorName = AuthorName;
int pageSize = 6;
int pageNumber = page ?? 1;
var result = db.Books.Where(s => s.Author_name.Contains(AuthorName)).OrderByDescending(x => x.Book_id).ToList();
return View(result.ToPagedList(pageNumber, pageSize));
}
return RedirectToAction("Index", "Home");
}
Author View Code:
#using PagedList.Mvc
#using System.Text.RegularExpressions;
#model PagedList.IPagedList<SmartBookLibrary.Models.Book>
#{
ViewBag.Title = "Books For " + ViewBag.AuthorName+ "- Smart Books Library";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#section AdditionalMeta
{
<meta name="robots" content="index,follow">
}
<div class="container">
<!-- Page Header -->
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
<span class="fa fa-pencil"></span>#ViewBag.AuthorName
</h1>
</div>
</div>
<!-- /.row -->
<!-- Projects Row -->
<div class="row">
#foreach (var item in Model)
{
<div class="col-md-4 portfolio-item" style="margin-bottom:10px;height:450px">
<div class="ih-item square effect3 bottom_to_top">
<a href="~/book/#item.Book_id/#item.UrlSlug">
<div class="img"><img itemprop="image" class="img-responsive" src="~/Images/#item.Book_Image" alt="#item.Book_name" title="#item.Book_name"></div>
<div class="info">
<h3>#item.Book_name</h3>
</div>
</a>
</div>
<h3 itemprop="name">
<a href="~/book/#item.Book_id/#item.UrlSlug">
#item.Book_name
#if (item.Edition != 0)
{
switch (item.Edition)
{
case 1:<small class="btn btn-default btn-xs">#item.Edition'St Edition</small> break;
case 2:<small class="btn btn-default btn-xs">#item.Edition'nd Edition</small> break;
case 3:<small class="btn btn-default btn-xs">#item.Edition'rd Edition</small> break;
case 4 - 10:<small class="btn btn-default btn-xs">#item.Edition'th Edition</small> break;
default:<small class="btn btn-default btn-xs">#item.Edition'th Edition</small> break;
}
}
</a>
</h3>
<p itemprop="description">
#*#Html.Raw(item.Book_Description)*#
</p>
</div>
}
</div>
<!-- /.row -->
<hr>
Page #(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of #Model.PageCount
#Html.PagedListPager(Model, page => Url.Action("Author", new { AuthorName = ViewBag.AuthorName, page }))
</div>
Calling Area :
#{
var s = Model.Book.Author_name.ToString();
string[] Auth = s.Split(',');
ViewBag.Author = Auth;
}
<p itemprop="author">
<i class="glyphicon glyphicon-pencil"></i> <b>Author Name :</b>
#for (int i = 0; i < Auth.Length; i++)
{
<a style="color:#777777" href="~/Author/#Auth[i]">#Auth[i];</a>
}
</p>
Any one know how to fix that ?
I test code in Author action without return View(result.ToPagedList(pageNumber, pageSize));
There is no problem.I think, ToPagedList has caused an error. result.ToPagedList(pageNumber, pageSize) is null.
You trace it.
I'm using mvc5 with razor view engine and I have a table of images in my DB , I want show 3 images in first line, then show some text in the next line, and then show 2 images in the next line with some text which will repeat by for loop like this image
my current code is :
<div style="padding: 0;">
#foreach (var item in Model.MainGoods)
{
<div class="col-sm-12 col-md-6 col-lg-4">
<a href="#">
<img src="#Url.Content(item.GoodImage.ToString())" />
</a>
</div>
}
</div>
Can anyone help me do it?
This should do the trick:
<div style="padding: 0;">
#{
var count =1;
<div class="row">
#foreach (var item in Model.MainGoods)
{
if (count <= 3)
{
<div class="col-md-4 well">
<a href="#">
<img src="#Url.Content(item.GoodImage.ToString())"/>
</a>
</div>
if (count == 3)
{
//new row
#Html.Raw("</div><div class=\"row\">")
}
}
else
{
<div class="col-md-6 well">
<a href="#">
<img src="#"/>
</a>
</div>
if (count == 5)
{
//new row
#Html.Raw("</div><div class=\"row\">")
}
}
if (count < 5)
{
//increment the count
count++;
}
else
{
//reset the count
count = 1;
}
}
</div>
}
</div>
I'm using MVC3 to create a beta mobile web app. It was working fine until I started to try and get inline pages inside of a view similar to this
This is what I currently have in my view called Index. How come when I click the links in the navbar it doesn't go to the page?
#model List<ShawGoVersion1.Models.NewsItem>
#{
ViewBag.Title = "News";
}
#section Header {
#Html.ActionLink("Back", "Index", "Home", null, new { data_icon = "arrow-l"})
<h1>#ViewBag.Title</h1>
<div data-role="navbar">
<ul>
<li>My News</li>
<li>Press Releases</li>
<li>All News</li>
</ul>
</div>
}
<div data-role="page" id="MyNews">
<div data-role="content">
<h1>This feature coming soon</h1>
</div>
</div>
<div data-role="page" id="AllNews">
<div data-role="content">
<ul data-role="listview">
#for (int i = 0; i < Model.Count; i++ )
{
if (Model[i].type == "Article")
{
<li>
<a href="#Url.Action("NewsItemDetails", "News", new { id = i })">
#if (Model[i].pictureURL != null)
{
<div class="ui-grid-c">
<div class="ui-block-a" style="width:31%;vertical-align:middle">
<img src="#Model[i].pictureURL" style="width:100%;height:auto"/>
</div>
<div class="ui-block-b" style="width:4%; vertical-align:middle"></div>
<div class="ui-block-c" style="width:65%;vertical-align:middle">
<h3 style="font-size:small; white-space:normal">#Model[i].title</h3>
<p><strong>#Model[i].date.ToShortDateString()</strong></p>
</div>
</div>
}
else
{
<h3 style="font-size:small; white-space:normal">#Model[i].title</h3>
<p><strong>#Model[i].date.ToShortDateString()</strong></p>
}
</a>
</li>
}
}
</ul>
</div>
</div>
<div id="PressReleases" data-role="page">
<div data-role"content">
<ul data-role="listview">
#for (int i = 0; i < Model.Count; i++ )
{
if (Model[i].type == "Press Release")
{
<li>
<a href="#Url.Action("NewsItemDetails", "News", new { id = i })">
#if (Model[i].pictureURL != null)
{
<div class="ui-grid-c">
<div class="ui-block-a" style="width:31%;vertical-align:middle">
<img src="#Model[i].pictureURL" style="width:100%;height:auto"/>
</div>
<div class="ui-block-b" style="width:4%; vertical-align:middle"></div>
<div class="ui-block-c" style="width:65%;vertical-align:middle">
<h3 style="font-size:small; white-space:normal">#Model[i].title</h3>
<p><strong>#Model[i].date.ToShortDateString()</strong></p>
</div>
</div>
}
else
{
<h3 style="font-size:small; white-space:normal">#Model[i].title</h3>
<p><strong>#Model[i].date.ToShortDateString()</strong></p>
}
</a>
</li>
}
}
</ul>
</div>
</div>
Your navbar should appear within data-role="page". Within the data-role="page" you should have a data-role="header" and your navbar should go in there if you want it to be in the header of the jqm page.
Alternatively if you don't want to do it that way(which you should do, as its the correct way). You could try catch the onclick event of those links in your navbar yourself and then fire
$.mobile.changePage();
This may work.