Display certain list when clicking on different links in a view - list

I need to display a different list each time I click on different links in my view. Help would be appreciated :)
My controller:
public class HomeController : Controller
{
Teams tm = new Teams();
Details det = new Details();
public ActionResult Index()
{
var model = new List<Teams>();
model.Add(new Teams { Name = "Manchester United", NickName = "The Red Devils", HomeGround = "Old Trafford", Founded = 1878 });
model.Add(new Teams { Name = "Liverpool", NickName = "The reds", HomeGround = "Anfield", Founded = 1870 });
return View(model);
}
public ActionResult About()
{
var title = new List<Details>();
title.Add(new Details { MajorHonours = 62, PremLeague = 20, FACup = 11, LeagueCup = 4, UEFA = 3 });
title.Add(new Details { MajorHonours = 60, PremLeague = 18, FACup = 7, LeagueCup = 8, UEFA = 5 });
return View();
}
My view with the links:
#model IEnumerable<Standings.Models.Teams>
#{
ViewBag.Title = "Standings";
}
<h1>List of teams</h1>
#foreach (var item in Model)
{
<div>
#Html.ActionLink(#item.Name, "About") (#item.NickName, #item.HomeGround, #item.Founded)
<hr />
</div>
}
My model:
public class Details
{
public int MajorHonours { get; set; }
public int PremLeague { get; set; }
public int FACup { get; set; }
public int LeagueCup { get; set; }
public int UEFA { get; set; }
}
And I have a clean View with the name About that the list needs to be displayed on

Related

How to get meta tags value from search engine optimization in SharePoint 2013

¿How to read meta tags in SharePoint 2013 in every page with friendly url?
Thanks in advance.
Ok,
I found the answer debuggin, this work for me.
public class SEOProperties {
public string PropBrowserTitle { get; set; }
public string PropBrowserDescription { get; set; }
public string PropBrowserKeyWords { get; set; }
public string PropBrowserSiteNoIndex { get; set; }
}
private SEOProperties GetSEOProperties(){
SEOProperties SEO = new SEOProperties();
if (SPContext.Current != null)
{
Guid siteGuid = SPContext.Current.Site.ID;
Guid webGuid = SPContext.Current.Web.ID;
using (SPSite site = new SPSite(siteGuid))
{
using (SPWeb web = site.OpenWeb())
{
TaxonomySession session = new TaxonomySession(site);
NavigationTerm navTermino = TaxonomyNavigationContext.Current.NavigationTerm;
Term termino = navTermino.GetTaxonomyTerm(session);
var SEOPropBrowserTitle = termino.LocalCustomProperties.Where(o => o.Key == "_Sys_Seo_PropBrowserTitle").SingleOrDefault();
var SEOPropDescription = termino.LocalCustomProperties.Where(o => o.Key == "_Sys_Seo_PropDescription").SingleOrDefault();
var SEOPropKeyWords = termino.LocalCustomProperties.Where(o => o.Key == "_Sys_Seo_PropKeywords").SingleOrDefault();
var SEOPropSiteNoIndex = termino.LocalCustomProperties.Where(o => o.Key == "_Sys_Seo_PropSitemapNoIndex").SingleOrDefault();
SEO.PropBrowserDescription = SEOPropDescription.Value;
SEO.PropBrowserKeyWords = SEOPropKeyWords.Value;
SEO.PropBrowserSiteNoIndex = SEOPropSiteNoIndex.Value;
SEO.PropBrowserTitle = SEOPropBrowserTitle.Value;
}
}
}
return SEO;
}

view model list return null object

view code:
#using HRHPMVS.Models
#model HRHPMVS.ViewModel.NationalityVM
#{
ViewBag.Title = "list";
//Layout = "~/Views/Shared/OrangeHR.cshtml";
Layout = null;
}
<h1>Details</h1>
<div>
<h1>Details</h1>
<div>
#if (Model.NationalitiesList != null)
{
foreach (var item in Model.NationalitiesList)
{
#Html.DisplayFor(m => item.Code)
}
}
</div>
</div>
controller code:
public ActionResult list()
{
ModelState.Clear();
NationRepObj.list();
return View();
}
model:
namespace HRHPMVS.Models
{
public class Nationality
{
public int ID { get; set; }
[Display(Name = "Name")]
[Required(ErrorMessage = "Name is Requirde")]
[RegularExpression(#"^[a-zA-Z]+$", ErrorMessage = "please: Use letters only ")]
public string Name { get; set; }
[Display(Name = "Code")]
[Required(ErrorMessage = "Code is Requirde")]
[RegularExpression(#"[0-9]*\.?[0-9]+", ErrorMessage = "{0} must be a Number.")]
[Range(0, int.MaxValue, ErrorMessage = "Please: enter valid integer Number")]
public int Code { get; set; }
public Nullable<short> IsActive { get; set; }
// ...
}
}
viewmodel:
namespace HRHPMVS.ViewModel
{
public class NationalityVM
{
public Nationality Nationality { get; set; }
public List<Nationality> NationalitiesList { get; set; }
// ...
}
}
viewmodellist:
namespace HRHPMVS.ViewModel
{
public class NationalityVMList
{
public List<NationalityVM> Nationalities {get;set;}
// ...
}
}
function :
public void list()
{
List<Nationality> n = new List<Nationality>();
Nationality nt = new Nationality { Code=1,Name="doodoo",ID=1,IsActive=1};
NationalityVM vm = new NationalityVM ();
List<NationalityVM> l1 = new List<NationalityVM>();
// foreach(var itm in nt)
n.Add(nt);
if (vm.NationalitiesList == null)
{
vm.NationalitiesList = new List<Nationality>();
vm.NationalitiesList.Add(nt);
}
}
I am trying to view detailed nationality in a view. I want to view it from listviewmodel but I failed. I made the viewmodel list point to view model and view model point to model but when I am trying to add nationality to list in return null value with error Null reference exception wasn't handled in user code.
I want to display nationality detail from viewmodel list
There are several issues with this code that it is hard to know what you are trying to achieve.
You have a class dubbed 'viewmodellist' called NationalityVMList. I don't believe this has a purpose. Maybe delete it.
Your view expects a model of type NationalityVM but your controller action passes nothing to it.
Your 'function' creates a list along with several unused variables and returns nothing.
Change your controller so it passes a model to the view:
public ActionResult list()
{
NationalityVM model = NationRepObj.GetNationalityVM();
ModelState.Clear();
return View(model);
}
Change your function to:
public NationalityVM GetNationalityVM()
{
NationalityVM vm = new NationalityVM();
Nationality nt = new Nationality { Code=1,Name="doodoo",ID=1,IsActive=1};
vm.NationalitiesList = new List<Nationality>();
vm.Add(nt);
return vm;
}
Hopefully this will get something working.

Moq unit test to filter products by their categories

I am new to unit testing so I am sure this is a very basic question, but I couldn't find a solution when I searched for it.
I am trying to test to see if I can filter products by their categories. I can access all the properties in my Product class but not the ones in my Category class. For example, it doesn't find Category1.Name. Can anyone tell me what I'm doing wrong?
This is my product class;
public partial class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public int CategoryID { get; set; }
public virtual Category Category1 { get; set; }
}
This is my test;
[TestMethod]
public void Can_Filter_Products()
{
//Arrange
Mock<IProductRepository> mock = new Mock<IProductRepository>();
mock.Setup(m => m.Products).Returns(new Product[]
{
new Product {ProductID=1,Name="P1", **Category1.Name** = "test1" },
new Product {ProductID=2,Name="P2", **Category1.Name** = "test2"},
new Product {ProductID=3,Name="P3", **Category1.Name** = "test1"},
new Product {ProductID=4,Name="P4", **Category1.Name** = "test2"},
new Product {ProductID=5,Name="P5", **Category1.Name** = "test3"},
}.AsQueryable());
//Arrange create a controller and make the page size 3 items
ProductController controller = new ProductController(mock.Object);
controller.PageSize = 3;
//Action
Product[] result = ((ProductsListViewModel)controller.List("test2", 1).Model).Products.ToArray();
//Assert - check that the results are the right objects and in the right order.
Assert.AreEqual(result.Length, 2);
Assert.IsTrue(result[0].Name == "P2" && result[0].Category1.Name == "test2");
Assert.IsTrue(result[1].Name == "P4" && result[1].Category1.Name == "test2");
}
In your mock setup, try this instead:
mock.Setup(m => m.Products).Returns(new[]
{
new Product {ProductID=1,Name="P1", Category1 = new Category { Name = "test1"} },
new Product {ProductID=2,Name="P2", Category1 = new Category { Name = "test1"} }
}.AsQueryable());

RavenDB MultiMapReduce Sum not returning the correct value

Sorry for this lengthy query, I decided to add the whole test so that it will be easier for even newbies to help me with this total brain-melt.
The using directives are:
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Raven.Client;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
Please leave feedback if I'm too lengthy, but what could possibly go wrong if I add a complete test?
[TestFixture]
public class ClicksByScoreAndCardTest
{
private IDocumentStore _documentStore;
[SetUp]
public void SetUp()
{
_documentStore = new EmbeddableDocumentStore {RunInMemory = true}.Initialize();
_documentStore.DatabaseCommands.DisableAllCaching();
IndexCreation.CreateIndexes(typeof (ClicksBySearchAndProductCode).Assembly, _documentStore);
}
[TearDown]
public void TearDown()
{
_documentStore.Dispose();
}
[Test]
public void ShouldCountTotalLeadsMatchingPreference()
{
var userFirst = new User {Id = "users/134"};
var userSecond = new User {Id = "users/135"};
var searchFirst = new Search(userFirst)
{
Id = "searches/24",
VisitId = "visits/63"
};
searchFirst.Result = new Result();
searchFirst.Result.Rows = new List<Row>(
new[]
{
new Row {ProductCode = "CreditCards/123", Score = 6},
new Row {ProductCode = "CreditCards/124", Score = 4}
});
var searchSecond = new Search(userSecond)
{
Id = "searches/25",
VisitId = "visits/64"
};
searchSecond.Result = new Result();
searchSecond.Result.Rows = new List<Row>(
new[]
{
new Row {ProductCode = "CreditCards/122", Score = 9},
new Row {ProductCode = "CreditCards/124", Score = 4}
});
var searches = new List<Search>
{
searchFirst,
searchSecond
};
var click = new Click
{
VisitId = "visits/64",
ProductCode = "CreditCards/122",
SearchId = "searches/25"
};
using (var session = _documentStore.OpenSession())
{
foreach (var search in searches)
{
session.Store(search);
}
session.Store(click);
session.SaveChanges();
}
IList<ClicksBySearchAndProductCode.MapReduceResult> clicksBySearchAndProductCode = null;
using (var session = _documentStore.OpenSession())
{
clicksBySearchAndProductCode = session.Query<ClicksBySearchAndProductCode.MapReduceResult>(ClicksBySearchAndProductCode.INDEX_NAME)
.Customize(x => x.WaitForNonStaleResults()).ToArray();
}
Assert.That(clicksBySearchAndProductCode.Count, Is.EqualTo(4));
var mapReduce = clicksBySearchAndProductCode
.First(x => x.SearchId.Equals("searches/25")
&& x.ProductCode.Equals("CreditCards/122"));
Assert.That(mapReduce.Clicks,
Is.EqualTo(1));
}
}
public class ClicksBySearchAndProductCode :
AbstractMultiMapIndexCreationTask
<ClicksBySearchAndProductCode.MapReduceResult>
{
public const string INDEX_NAME = "ClicksBySearchAndProductCode";
public override string IndexName
{
get { return INDEX_NAME; }
}
public class MapReduceResult
{
public string SearchId { get; set; }
public string ProductCode { get; set; }
public string Score { get; set; }
public int Clicks { get; set; }
}
public ClicksBySearchAndProductCode()
{
AddMap<Search>(
searches =>
from search in searches
from row in search.Result.Rows
select new
{
SearchId = search.Id,
ProductCode = row.ProductCode,
Score = row.Score.ToString(),
Clicks = 0
});
AddMap<Click>(
clicks =>
from click in clicks
select new
{
SearchId = click.SearchId,
ProductCode = click.ProductCode,
Score = (string)null,
Clicks = 1
});
Reduce =
results =>
from result in results
group result by
new { SearchId = result.SearchId, ProductCode = result.ProductCode }
into g
select
new
{
SearchId = g.Key.SearchId,
ProductCode = g.Key.ProductCode,
Score = g.First(x => x.Score != null).Score,
Clicks = g.Sum(x => x.Clicks)
};
}
}
public class User
{
public string Id { get; set; }
}
public class Search
{
public string Id { get; set; }
public string VisitId { get; set; }
public User User { get; set; }
private Result _result = new Result();
public Result Result
{
get { return _result; }
set { _result = value; }
}
public Search(User user)
{
User = user;
}
}
public class Result
{
private IList<Row> _rows = new List<Row>();
public IList<Row> Rows
{
get { return _rows; }
set { _rows = value; }
}
}
public class Row
{
public string ProductCode { get; set; }
public int Score { get; set; }
}
public class Click
{
public string VisitId { get; set; }
public string SearchId { get; set; }
public string ProductCode { get; set; }
}
My problem here is that I expect Count to be one in that specific test, but it just doesn't seem to add the Clicks in the Click map and the result is 0 clicks. I'm totally confused, and I'm sure that there is a really simple solution to my problem, but I just can't find it..
..hope there is a week-end warrior out there who can take me under his wings.
Yes, it was a brain-melt, for me non-trivial, but still. The proper reduce should look like this:
Reduce =
results =>
from result in results
group result by
new { SearchId = result.SearchId, ProductCode = result.ProductCode }
into g
select
new
{
SearchId = g.Key.SearchId,
ProductCode = g.Key.ProductCode,
Score = g.Select(x=>x.Score).FirstOrDefault(),
Clicks = g.Sum(x => x.Clicks)
};
Not all Maps had the Score set to a non-null-value, and therefore my original version had a problem with:
Score = g.First(x => x.Score != null).Score
Mental note, use:
Score = g.Select(x=>x.Score).FirstOrDefault()
Don't use:
Score = g.First(x => x.Score != null).Score

Cannot get google visualisation/graph to load up via Ajax call?

Is there a special way of creating a google chart via an Ajax call, which is different from the static method?
The HTML i am producing is correct because it will load from a normal HTML file, but when im calling the Ajax, the data in the graph is not showing.
I am using google.setOnLoadCallback() and google.load('visualization', '1', {packages: ['table']})
You need to get data from ajax call and then put it in to your visualization function.
Here is my code:
google.load('visualization', '1', { packages: ['corechart'] });
google.setOnLoadCallback(OnLoad);
var url = '/Charting/GetData';
function OnLoad() {
$.ajax({
url: url,
dataType: 'json',
success: function (response) {
drawVisualization(response);
}
});
};
function drawVisualization(response) {
var chart = new google.visualization.ColumnChart(
document.getElementById('visualization'));
var data = new google.visualization.DataTable(response);
chart.draw(data);
};
Also i recommend you to use this class to generate correct JSON response:
public class ChartHelper
{
public ColInfo[] cols { get; set; }
public DataPointSet[] rows { get; set; }
}
public class ColInfo
{
public string id { get; set; }
public string label { get; set; }
public string type { get; set; }
}
public class DataPointSet
{
public DataPoint[] c { get; set; }
}
public class DataPoint
{
public object v { get; set; } // value
public string f { get; set; } // format
}
Then you can use it like this:
[ActionName("data")]
public JsonResult Data()
{
Random r = new Random();
var graph = new ChartHelper
{
cols = new ColInfo[] {
new ColInfo { id = "A", label = "Name", type = "string" },
new ColInfo { id = "B", label = "Value", type = "number" },
},
rows = new DataPointSet[] {
new DataPointSet {
c = new DataPoint[]
{
new DataPoint { v = "Name" },
new DataPoint { v = r.NextDouble()},
}},
new DataPointSet {
c = new DataPoint[]
{
new DataPoint { v = "Name2" },
new DataPoint { v = r.NextDouble()},
}},
new DataPointSet {
c = new DataPoint[]
{
new DataPoint { v = "Name3" },
new DataPoint { v = r.NextDouble()},
}}
}
};
return Json(graph, JsonRequestBehavior.AllowGet);
}