I want the number of customers in that country to be written in the numbers "00" next to the countries. can you help?
With the html helper, I can classify suppliers on the basis of countries. but I could not write the number of suppliers in that country on the same line.
controller:
public ActionResult SupplierList(int supID = 0, string country="")
{
ViewBag.supCountries= _db.Suppliers.Select(a => a.Country).Distinct().ToList();
List<Suppliers> supplierList;
if (country== "" && supID == 0)
{
supplierList = _db.Suppliers.ToList();
}
else if (country!= "" && supID == 0)
{
supplierList = _db.Suppliers.Where(a => a.Country == country).ToList();
}
else
{
supplierList= _db.Suppliers.Where(a => a.SupplierID == supID).ToList();
}
return View(supplierList);
}
view:
<div class="col-md-2">
<ul class="list-group">
#foreach (string item in ViewBag.supCountries)
{
<li class="list-group-item">
#Html.ActionLink(item, "SupplierList", new { country= item })
<span style="float:right; color:lightgrey">
00
</span>
</li>
}
</ul>
<div>
#Html.ActionLink("New Supplier", "Create", null, new { #class = "btn btn-success" })
</div>
</div>
You passed the supplier list in as a model, so you should be able to replace 00 with something like
<span style="float:right; color:lightgrey">
#Model.Count(x => x.Country == item).ToString("N0")
</span>
Related
I've a model that contains 3 tables in my view.
public class InExam
{
public AutoTests TheTest { get; set; }
public List<InTest> TheQuestions { get; set; }
public IEnumerable<Result> SingleQuee { get; set; }
}
First one made to get the detailed page, like "admin/AutoTests/id"
Second one made to get a list of questions linked to the page
Third one is to save radio button strings to post it back into the controller
my plan is to get (say) 20 questions that are linked with the detailed page, Adding 4 radio buttons for each question, and post back every selected button to the controller.
my view form :
#using (Html.BeginForm("Test", "Exams", new { id = Model.TheTest.id }, FormMethod.Post))
{
foreach (var item in Model.TheQuestions)
{
Kafo.Models.Result singleQuee = Model.SingleQuee.Where(x => x.Question == item.Question).FirstOrDefault();
<div class="container" style="padding-top:50px;direction:rtl;">
<h4 style="text-align:right;font-weight:bold;">#item.Question</h4>
<div class="container">
<div class="row" style="direction:rtl;">
<div class="col-lg-7" style="text-align:right;margin-right:10px;">
<div class="row">
#Html.RadioButtonFor(x => singleQuee.Question, new { #class = "form-control dot", #Name = singleQuee.Question, #Value = "1" })
<h5 style="padding-top:3px;padding-right:8px;">#item.RightAnswer</h5>
</div>
</div>
<div class="col-lg-7" style="text-align:right;margin-right:10px;">
<div class="row">
#Html.RadioButtonFor(x => singleQuee.Question, new { #class = "form-control dot", #Name = singleQuee.Question, #Value = "2" })
<h5 style="padding-top:3px;padding-right:8px;">#item.Answer2</h5>
</div>
</div>
<div class="col-lg-7" style="text-align:right;margin-right:10px;">
<div class="row">
#Html.RadioButtonFor(x => singleQuee.Question, new { #class = "form-control dot", #Name = singleQuee.Question, #Value = "3" })
<h5 style="padding-top:3px;padding-right:8px;">#item.Answer3</h5>
</div>
</div>
<div class="col-lg-7" style="text-align:right;margin-right:10px;">
<div class="row">
#Html.RadioButtonFor(x => singleQuee.Question, new { #class = "form-control dot", #Name = singleQuee.Question, #Value = "4" })
<h5 style="padding-top:3px;padding-right:8px;">#item.Answer4</h5>
</div>
</div>
#Html.HiddenFor(m => singleQuee.Question)
</div>
</div>
</div>
}
<button class="btn botton" type="submit" onclick="return confirm('');">END</button>
}
i used this line "Kafo.Models.Result singleQuee = Model.SingleQuee.Where(x => x.Question == item.Question).FirstOrDefault();" in my view because i can't use tuple foreach ( C# ver. 5 )
This is my controller code :
[HttpGet]public ActionResult Test(int? id)
{
using (KafoEntities db = new KafoEntities())
{
InExam model = new InExam();
model.TheTest = db.AutoTests.Where(x => x.id == id).FirstOrDefault();
model.TheQuestions = db.InTest.Where(x => x.UserEmail == currentUser.Email && x.ExamId == model.TheTest.id).OrderByDescending(x => x.id).Take(Convert.ToInt32(model.TheTest.QuestionsNumber)).ToList();
model.SingleQuee = db.Result.ToList();
return View(model);
}
}
[HttpPost]
public ActionResult Test(int? id, List<Result> singleQuee)
{
using (KafoEntities db = new KafoEntities())
{
int result = 0;
foreach (Result item in singleQuee)
{
Result sets = db.Result.Where(x => x.id == item.id).FirstOrDefault();
sets.Question = item.Question;
db.SaveChanges();
var check = db.InTest.Where(x => x.Question == item.Question).FirstOrDefault();
if (check != null)
{
if (item.Question == "1")
{
result++;
}
}
}
return RedirectToAction("Results", "Exams", new { Controller = "Exams", Action = "Results", id = done.id });
}
}
I first save the new string that came from the radio button value into the result record, then i call it back in the if condition to check it's value
The problem here is i get a
Object reference not set to an instance of an object.
when i post the test, it means that the list is empty, so i need to know what makes the radio buttons not working,
Thanks.
If you want to bind a List of object in Mvc, you should name the controller like "ModelName[indx].PropertyName". In your case it should be "singleQuee[0].Question".
Code Sample
var Indx = 0;
foreach (var item in Model.TheQuestions)
{
.....
var radioName = $"singleQuee[{Indx}].Question";
<div class="col-lg-7" style="text-align:right;margin-right:10px;">
<div class="row">
<input type="radio" name="#radioName" value="1" />
<h5 style="padding-top:3px;padding-right:8px;">#item.RightAnswer</h5>
</div>
</div>
.....
}
Action Method
I've written some ActionLinks to filter the data on a movieshop base on genre while using PagedList.Mvc for paging.
The problem is that the ActionLinks work properly,but only filter the movies existing on the first page. What can I do?
View:
#using PagedList.Mvc
#using Shop.CustomHtmlHelpers
#using Shop.Domain.Entities
#model PagedList.IPagedList<Shop.Domain.Entities.Movie>
#{
ViewBag.Title = "MovieDisplay";
}
<h2>MovieDisplay</h2>
#using (#Html.BeginForm("MovieDisplay", "Movies", FormMethod.Get))
{
<div class="container">
<div class="row">
<div class="col-md-10">
#foreach (Product p in Model)
{
var item = (Movie)p;
<div class="col-lg-3 col-md-3 col-sm-4 col-xs-6">
<div class="tile">
<br />
<b>#item.Title</b>
<br />
#item.Year
<br/>
#item.Genre
<br/>
#item.Director.Name
#Html.Image(#item.Image,"200")
<br />
$ #item.Price
<br/>
<br />
<button onclick="location.href = '#Url.Action("AddtoCart", "ShoppingCart", new {id = p.Id})'" type="button" class="btn btn-success btn-su btn-sm ">
<span> <i class="fa fa-shopping-basket" aria-hidden="true"></i></span> AddtoCart
</button>
<br />
<br /><br />
</div>
</div>
}
</div>
</div>
</div>
}
#Html.ActionLink("Horror", "MovieDisplay", new { Genre = "Horror" } )
#Html.ActionLink("Action", "MovieDisplay", new { Genre = "Action" })
#Html.ActionLink("Drama", "MovieDisplay", new { Genre = "Drama" })
#Html.ActionLink("Animation", "MovieDisplay", new { Genre = "Animation" })
#Html.ActionLink("Comedy", "MovieDisplay", new { Genre = "Comedy" })
#Html.ActionLink("Crime", "MovieDisplay", new { Genre = "Crime" })
#Html.ActionLink("Sci-Fi", "MovieDisplay", new { Genre = "Sci-Fi" })
#Html.ActionLink("Fantasy", "MovieDisplay", new { Genre = "Fantasy" })
#Html.ActionLink("Historical", "MovieDisplay", new { Genre = "Historical" })
#Html.ActionLink("Musical", "MovieDisplay", new { Genre = "Musical" })
#Html.PagedListPager(Model, page => Url.Action("MovieDisplay", new { page, searchTerm = Request.QueryString["searchTerm"], Genre = "Genre" }))
Related ActionResult:
public ActionResult MovieDisplay(string searchTerm, int? page , string genre)
{
MediaContext mc = new MediaContext();
var movies = mc.Movies.ToList().ToPagedList(page ?? 1, 6);
if(genre !=null)
movies = movies.Where(m => m.Genre == genre).ToList().ToPagedList(page ?? 1, 6);
if (string.IsNullOrEmpty(searchTerm))
{ }
else
movies = mc.Movies.Where(x => x.Title.Contains(searchTerm)).ToList().ToPagedList(page ?? 1, 6);
return View(movies);
}
The problem was nothing but a silly mistake I made in the ActionResult :
movies = movies.Where(m => m.Genre == genre).ToList().ToPagedList(page ?? 1, 6);
Here I actually filter the existing movies ! I should have added the whole movies to the list just like I did for the searchTerm (that's why the search worked properly for all the pages)
movies = mc.Movies.Where(m => m.Genre == genre).ToList().ToPagedList(page ?? 1, 6);
I have a problem with a partial view. The first time when it is rendered everything is ok, but after that some data is loosed.
This is how my page should look: this is when the partial view is rendered the first time
but when I'm clicking on a category to check his subcategories, the image is null, it is not visible anymore. (only the name is visible)
This is my partial view:
#model IEnumerable<OnlineCarStore.Models.CategoriesVM>
<div class="container">
<div class="row">
#using (Html.BeginForm("SubCategories", "Product"))
{
<div class="list-group col-sm-3" style="width:280px;">
#{var selected = string.Empty;
if (#HttpContext.Current.Session["selectedCar"] == null)
{
selected = string.Empty;
}
else
{
selected = #HttpContext.Current.Session["selectedCar"].ToString();
}
foreach (var c in Model)
{
<a href="#Url.Action("SubCategories", "Product", new { selected = #selected, id = #c.ID, category = #c.CategoryName })" class="list-group-item">
<span> #c.CategoryName</span>
</a>
}
}
</div>
<div class="col-sm-9">
#foreach (var c in Model)
{
<div class="card-group" style="width:200px; display: inline-block">
<div class="card card">
<a href="#Url.Action("SubCategories", "Product", new { selected = #HttpContext.Current.Session["selectedCar"].ToString(), id = #c.ID, category = #c.CategoryName })" id="linkOnImg" class="card-group">
<img class="card-img-top center-block" src="#string.Format("../content/images/categories/{0}.jpg", #c.AtpID)" alt=#c.CategoryName style="padding-top: 5px">
<div class="card-text text-center">
<p class="category-card-title ">
<span class="text-muted">#c.CategoryName</span>
</p>
</div>
</a>
</div>
</div>
}
</div>
}
</div>
and this is the Subcategoris view, where the partial view is rendered second time:
<div class="container">
<div class="row">
#Html.Partial("/Views/Home/CategorieTypes.cshtml");
Here is the Product controller Subcategories method:
public ActionResult SubCategories(string selected, int id, string category)
{
List<CategoriesVM> listOfCategories = new List<CategoriesVM>();
var list = db.Categories.ToList();
var root = list.GenerateTree(c => c.ID, c => c.ParentId).ToList();
TreeItem<Categories> found = null;
var test = new TreeItem<Categories>();
foreach (var rootNode in root)
{
found = TreeGenerator.Find(rootNode, (n) => n.Item.ID == id);
if (found != default(TreeItem<Categories>))
{
test = found;
break;
}
}
foreach (var item in found.Children.ToList())
{
CategoriesVM categoryv = new CategoriesVM();
categoryv.ID = item.Item.ID;
categoryv.AtpID = item.Item.AtpID;
categoryv.Childrens = item.Children.ToList();
categoryv.CategoryName = item.Item.AtpName;//.Name;
listOfCategories.Add(categoryv);
}
SiteMaps.Current.CurrentNode.Title = selected + " " + category;
var tests = found.Children.ToList();
if (found.Children.ToList().Count == 0 )
{
return View("Index");
}
else
{
return View("SubCategories", listOfCategories);
}
}
I've checked in developer tool if the source for the image is correct, and although it is correct, the image is not showed:
Can you please help me in this problem? What I do wrong? Why the images don't appear after the first render of the partial view? Thanks in advance!
The path of the partial view is /Views/Home/CategorieTypes.cshtml and the path of the images is /Content/images/categories. In the partial view you are using ../content/images/categories/ as the path of the images which means that it will search for the path /Views/Content/Images/Categories which is invalid.
Remove the two dots in the src property and add a ~ so it will be like: ~/Content/Images/Categories/{img}.
or
Add one more ../ in order to go one level down to the directory like:
../../Content/Images/Categories/{img}
I have an MVC menu that is being populated from the database. I have code for it to reach the 4th menu child. I am wondering if there is a better way to write this code in my cshtml so that it detects menu children automatically and i dont have to hard code the menu to a certain level. Thanks for your help.
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
#foreach (var menuLevel1 in navbarmenu)
{
<li class="dropdown">
<a href="~/CATS/Default.aspx">
#menuLevel1.MenuName<span class="caret"></span>
</a>
#{var navbarsubmenu = myMenu.Where(x => x.ParentID == menuLevel1.MenuID && x.IsDeleted == false && x.IsEnabled == true).ToList().OrderBy(x => x.MenuName);}
#if (navbarsubmenu.IsAny())
{
<ul class="dropdown-menu">
#foreach (var menuLevel2 in navbarsubmenu)
{
<li class="dropdown-submenu">
#if (menuLevel2.MenuLink != null && menuLevel2.MenuLink != "")
{
<a href="#Url.Content(menuLevel2.MenuLink)">
#menuLevel2.MenuName
</a>
}
else
{
<a href="~/CATS/Default.aspx">
#menuLevel2.MenuName
</a>
}
#{var navbarsubmenu2 = myMenu.Where(x => x.ParentID == menuLevel2.MenuID && x.IsDeleted == false && x.IsEnabled == true).ToList().OrderBy(x => x.MenuName);}
#if (navbarsubmenu2.IsAny())
{
<ul class="dropdown-menu">
#foreach (var menuLevel3 in navbarsubmenu2)
{
<li class="dropdown-submenu">
#if (menuLevel3.MenuLink != null && menuLevel3.MenuLink != "")
{
<a href="#Url.Content(menuLevel3.MenuLink)">
#menuLevel3.MenuName
</a>
}
else
{
<a href="~/CATS/Default.aspx">
#menuLevel3.MenuName
</a>
}
#{var navbarsubmenu3 = myMenu.Where(x => x.ParentID == menuLevel3.MenuID && x.IsDeleted == false && x.IsEnabled == true).ToList().OrderBy(x => x.MenuName);}
#if (navbarsubmenu3.IsAny())
{
<ul class="dropdown-menu">
#foreach (var menuLevel4 in navbarsubmenu3)
{
<li class="dropdown-submenu">
#if (menuLevel4.MenuLink != null && menuLevel4.MenuLink != "")
{
<a href="#Url.Content(menuLevel4.MenuLink)">
#menuLevel4.MenuName
</a>
}
else
{
<a href="~/CATS/Default.aspx">
#menuLevel4.MenuName
</a>
}
</li>
}
</ul>
}
</li>
}
</ul>
}
</li>
}
</ul>
}
</li>
}
</ul>
Recursive Method as recommended by StephenMuecke
public static class MenuChildExtensions
{
public static MvcHtmlString MenuChild(this HtmlHelper helper, Menu menu)
{
return MvcHtmlString.Create(MenuChildren(menu));
}
private static string MenuChildren(Menu menu)
{
StringBuilder html = new StringBuilder();
TagBuilder div = new TagBuilder("div");
div.InnerHtml = menu.Name;
html.Append(div.ToString());
if (menu.MenuItems != null)
{
foreach (Menu menuchildMenu in menu.MenuItems)
{
html.Append(MenuChildren(menuchildMenu));
}
}
TagBuilder item = new TagBuilder("li");
item.InnerHtml = html.ToString();
TagBuilder container = new TagBuilder("ul");
container.InnerHtml = item.ToString();
return container.ToString();
}
}
}
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.