EPiServer 7 namespace for Locate() isn't resolving - asp.net-mvc

I'm new to EPiServer, and am attempting to retrieve child news article pages from a news list that I created. Examples that I've found online used the Locate() method, but when I attempt to apply Locate to my code, it's not being found.
This is one of the articles that I looked at.
http://world.episerver.com/Blogs/Johan-Bjornfot/Dates1/2012/8/EPiServer7-Working-with-IContentRepositoryDataFactory/
Essentially, I just need to return a list of child articles for a list of news items, so it's possible that the approach that I'm attempting is not right in the first place.
At any rate, this is my current model with the using statements.
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.ServiceLocation;
using EPiServer.SpecializedProperties;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace EPiServerExercise.Models.Pages
{
[ContentType(DisplayName = "News List", GUID = "ac3287b1-4d78-4eb3-bad2-6b5c43530b33", Description = "")]
public class NewsList : BasePage
{
private IEnumerable<NewsArticle> getNewsArticles(NewsList currentPage)
{
//var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>
//IEnumerable<NewsArticle> newsArticles = new List<NewsArticle>();
//PageReference pageLink = currentPage.ParentLink;
//IEnumerable<NewsArticle> newsArticles = Locate.ContentRepository().GetChildren<IContent>(pageLink);
//IEnumerable<NewsArticle> newsArticles = ServiceLocationHelperExtensions.
//var serviceLocationHelper = ServiceLocator.Current.GetInstance();
//serviceLocationHelper.ContentLoader
}
}
}
What reference am I missing to get the Locate() method to resolve? We are using EPiServer 7 and MVC. Thanks for your help.
Update on 11/18/2014
This is the eventual solution that I put into the model. It's almost identical to what Vsevolod Goloviznin suggested. Thanks.
public string showNewsArticles()
{
IEnumerable<NewsArticle> newsArticles = getNewsArticles(this);
// Code to loop through the articles
}
private IEnumerable<NewsArticle> getNewsArticles(NewsList currentPage)
{
var repository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
IEnumerable<NewsArticle> newsArticles = repository.GetChildren<NewsArticle>(currentPage.ContentLink);
return newsArticles;
}

Looks like he just uses a factory to get IContentRepository and forgot to mention to. So to just get the same functionality you can use the ServiceLocator to get the IContentRepository and then get all children for your page:
var service = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
var pages = service.GetChildren<NewsArticle>(currentPage.ParentLink).ToList();

Related

Cast List<ViewModel> to ViewModel?

I want to preface this by saying I am new to working with models. Please forgive me if this question has a simple answer.
I have been struggling to revert a listed view model back to view model. To give some background, I have a search form being passed to a model coming from my ActionResult and am then getting a filter out the results.
[ Controller ]
public ActionResult GetFilters(MembershipVM model)
{
var uDataList = new List<MembershipVM>();
model = _service.GetFilters(model);
return View("SendEmail", model);
}
[ Service ]
public List<MembershipVM> GetFilters(MembershipVM model)
{
var query = _context.Members.Where(f => f.Deleted == 0).AsQueryable();
var members = _context.Members.ToList();
query = query.Where(f => agencyTypes.Contains(f.AgencyType));
var uDataList = new List<MembershipVM>();
foreach (var member in members)
{
var uData = new MembershipVM();
uData.Email = member.Email;
uData.AgencyType = member.AgencyType;
...
uDataList.Add(uData);
}
return uDataList;
}
How can I cast the List from "_service.GetFilters" to MembershipVM? Is there a better/easier way to get the results as an object from the "_service.GetFilters" service?
Thanks so much in advance!
Daisy
I am not sure what you are trying to do here. First you get the results of your filter from this code:
model = _service.GetFilters(model);
And the definition of your method is this:
public List<MembershipVM> GetFilters(MembershipVM model)
So you would expect that this is a list of results. In short, a collection of results.
Now if you want to pass it on your ActionResult as one entity only, then getting one of your results will do the trick:
return View("SendEmail", model.Take(1).SingleOrDefault());
But why do you need to pass one entity only? But that should work with your current requirement.

Access data from Model in Controller?

I have a model for displaying a list of latest articles, and I would like to be able to define how many articles are displayed. I have created a property called DisplayNumberOfPressArticles and I would like to be able to access the value of this property from my controller.
Here is the model:
using Site.Helpers.Selections;
using Site.Models.Blocks.Data;
using EPiServer.DataAnnotations;
using EPiServer.Shell.ObjectEditing;
using System.ComponentModel.DataAnnotations;
namespace Site.Models.Blocks
{
[SiteContentType(GUID = "884202C2-61F1-4DF5-85A7-DC3D4E493F59")]
[SiteImageUrl]
public class LastArticlesTeaserBlock : SiteBlockData, PressOverviewData, ISpaceableData
{
[CultureSpecific]
public virtual string Heading { get; set; }
[Range(1, 1000)]
public virtual int DisplayNumberOfPressArticles { get; set; }
}
}
In my controller, I would like to take the value of DisplayNumberOfPressArticles as a limit for the .Take() query:
using System;
using System.Collections.Generic;
using Site.Models.Blocks;
using EPiServer.Web.Mvc;
using System.Web.Mvc;
using Site.Models.Pages;
using Site.Models.ViewModels;
using EPiServer.Find;
using EPiServer.Find.Api;
using EPiServer.Find.Cms;
using EPiServer.Find.Framework;
using EPiServer.Logging;
using ILogger = EPiServer.Logging.ILogger;
using EPiServer.Globalization;
namespace Site.Controllers
{
public class LastArticlesTeaserBlockController : BlockController<LastArticlesTeaserBlock>
{
private static readonly ILogger Logger = LogManager.GetLogger();
public override ActionResult Index(LastArticlesTeaserBlock currentContent)
{
var model = new LastArticlesTeaserViewModel
{
Headline = currentContent.Heading,
ArticlePages = GetArticlePages(),
PaddingTop = currentContent.PaddingTop,
PaddingBottom = currentContent.PaddingBottom
};
return PartialView(model);
}
private List<PressDetailsPage> GetArticlePages()
{
List<PressDetailsPage> result = new List<PressDetailsPage>();
IClient findClient = SearchClient.Instance;
var search = findClient.Search<PressDetailsPage>();
search = search
.Filter(sp => sp.IsDeleted.Match(false)).PublishedInCurrentLanguage()
.Filter(sp => sp.Headline.Exists() | sp.Description.Exists())
.Filter(sp => sp.Language.Name.Match(ContentLanguage.PreferredCulture.Name))
.OrderByDescending(sp => sp.PublishDate, SortMissing.Last)
.Take(??);
try
{
var searchResult = search.GetContentResult();
result.AddRange(searchResult);
}
catch (NullReferenceException e)
{
Logger.Error(e.ToString());
}
return result;
}
}
}
Sorry for the newbie question, but everything I have tried hasn't worked so far. I thought I could access the model by using .Take(LastArticlesTeaserBlock.DisplayNumberOfPressArticles);
How about changing your signature to something like GetArticlePages(int maxCount) and then invoking it like ArticlePages = GetArticlePages(currentContent.DisplayNumberOfPressArticles) in your Index method?
Your SearchClient class looks like a custom implementation and hard to tell if the bug is there.
Option 1:
Something like -- db.ArticleSet.OrderByDescending(t => t.Articles.Count).Take(10);
Option 2:
Or a more direct query.
Make life easier and break it down in two parts firs the query, they execution, then put it back together if you want.
Step 1
var articlesQuery = from x in Articles..
where x.IsDeleted == true ....
Step 2
var limitedArticlesQuery = articlesQuery.Take(25);
From client size send a int pageSize
var pagedProductQuery = articlesQuery.Skip(10 * pageSize).Take(10)

Create new content nodes programmatically in Umbraco 8

In Umbraco 7 I used the following code to generate code programmatically from C# (controller)
using ContentService.CreateContent
And following is the code for the same
int parentID = 1100;
var request = ContentService.CreateContent("New Node Name", parentID, ContactUsForm.ModelTypeAlias);
request.SetValue(ContactRequestItem.GetModelPropertyType(C => C.FirstName).PropertyTypeAlias, FormModel.FirstName);
ContentService.PublishWithStatus(request);
Now in Umbraco 8
it is asking for
Udi ParentId
getting error "Can not convert 'int' to 'Umbraco.Core.Uid' ".
Have searched a lot, but can't find anything for Umbraco 8.
So now the question is How we can create a node from a controller in Umbraco 8?
How about getting the parent node first (this can be done via int ID) and then get the UDI from that? Something like
var parent = ContentService.GetById(1100);
var request = ContentService.CreateContent("New Node Name", parent.GetUdi(), ContactUsForm.ModelTypeAlias);
The solution is as suggested on the following the link
on the Umbraco Forum
public IContentService _contentService { get; set; }
public TestController(IContentService contentService)
{
_contentService = contentService;
}
public override ActionResult Index(ContentModel model)
{
var parentId = new Guid("3cce2545-e3ac-44ec-bf55-a52cc5965db3");
var request = _contentService.Create("test", parentId, ContentPage.ModelTypeAlias);
_contentService.SaveAndPublish(request);
return View();
}
In Umbraco 8, you need the parent Udi to create a new node. You can do this by getting the parent node first then getting the Udi using the parent node like this:
var parentNode = ContentService.GetById(1100);
var parentUdi = new GuidUdi(parentNode.ContentType.ToString(), parentNode.Key);
You can then call the CreateContent method and pass in the parentUdi as a parameter:
var request = ContentService.CreateContent("New Node Name", parentUdi, ContactUsForm.ModelTypeAlias);
ContentService.SaveAndPublish(request);

Getting error when multiple results passing to view in asp.net MVC5

I use asp.net mvc 5 and EF 6 .I have following code in my controller
var resultOne = stats.GetPlayerStatsByParam(year, modeOne); //first
ViewData["more"] = stats.GetTeamStatsID(year); //second
return View("StatsNew", resultOne.ToList());
I am able to display result in view using "resultOne". Now I want to pass another data to same view using ViewData[]. its a stored procedure and the result is paasing to "ViewData["more"]".
I have done following code for "ViewData["more"]" in View page
But I am getting an error saying that 'object reference not set'
#foreach (var item in ViewData["more"] as #List<demo.GetTeamStatsID_Result>)
{
#item.Pld;
}
Use a viewmodel and extend it by the data coming from stats.GetTeamStatsID(year);
Do not use ViewBag or ViewData if not necessary for some reason (which I canĀ“t imagine right now)
As the comments have already pointed out, build a ViewModel class, that wraps everthing you need:
//I am using fake types here, sinceI don't know your model classes
//substitute accordingly!
public class MyViewModel
{
public PlayerStatsType PlayerStats { get; set;}
public List<demo.GetTeamStatsID_Result> Teams { get; set;}
}
Then in your action method:
var vm = new MyViewModel();
vm.PlayerStats = stats.GetPlayerStatsByParam(year, modeOne); //first
vm.TeamId = stats.GetTeamStatsID(year); //second
return View("StatsNew", vm);
Amend the model declaration in your view:
#model Namespace.Models.MyViewModel //again use namespace for your vm class
Now you can access both properties from your model:
#foreach (var item in Model.Teams)
{
#item.Pld;
}

What templating library can be used with Asp .NET MVC?

In my MVC 5 app I need to be able to dynamically construct a list of fully qualified external URL hyperlinks, alone with some additional data, which will come from the Model passed in. I figure - I will need to construct my anchor tags something like this:
{{linkDisplayName}}
with AngularJS this would be natural, but, I have no idea how this is done in MVC.
Is there a templating library that can be used for this?
1) Create a model to Hold the Links
public class LinkObject
{
public string Link { get; set; }
public string Description { get; set; }
}
2) In your Action you can use ViewBag, ViewData or even pass the list inside you Model. I will show you how to do using ViewBag
public ActionResult MyDynamicView()
{
//Other stuff and code here
ViewBag.LinkList = new List<LinkObject>()
{
new LinkObject{ Link ="http://mylink1.com", Description = "Link 1"},
new LinkObject{ Link ="http://mylink2.com", Description = "Link 2"},
new LinkObject{ Link ="http://mylink3.com", Description = "Link 3"}
};
return View(/*pass the model if you have one*/);
}
3) In the View, just use a loop:
#foreach (var item in (List<LinkObject>)ViewBag.LinkList)
{
#item.Description
}
Just create a manual one for that, no need to do it from a template. For example, in javascript
function groupAnchor(url,display){
var a = document.createElement("a");
a.href = url;
a.className = "list-group-item";
a.target = "_blank";
a.innerHTML = display;
return a;
}
And then use that function to modify your html structure
<div id="anchors"></div>
<script>
document.getElementById("anchors").appendChild(groupAnchor("http://google.com","Google"));
</script>
Your approach to modification will more than likely be more advanced than this, but it demonstrates the concept. If you need these values to come from server side then you could always iterate over a set using #foreach() and issue either the whole html or script calls there -- or, pass the set from the server in as json and then use that in a function which is set up to manage a list of anchors.
To expand on this, it is important to avoid sending html to the view from a razor iteration. The reason being that html constructed by razor will increase the size of the page load, and if this is done in a list it can be a significant increase.
In your action, construct the list of links and then serialize them so they can be passed to the view
public ActionResult ViewWithLinks()
{
var vm = new ViewModel();
vm.Links = Json(LinkSource.ToList()).Data;
//or for a very simple test for proof of concept
var Numbers = Json(Enumerable.Range(0,100).ToList()).Data;
ViewData["numbers"] = Numbers ;
return View(vm);
}
where all you need is an object to hold the links in your view model
public class ViewModel
{
public ICollection<Link> Links { get; set; }
}
public class Link
{
public string text { get; set; }
public string href { get; set; }
}
and then in your view you can consume this json object
var allLinks = #Html.Raw(Json.Encode(Model.Links));
var numbersList = #Html.Raw(Json.Encode(ViewData["linkTest"]));//simple example
Now you can return to the above function in order to place it on the page by working with the array of link objects.
var $holder = $("<div>");
for(var i = 0; i < allLinks.length; i++){
$holder.append(groupAnchor(allLinks[i].href,allLinks[i].text));
}
$("#linkArea").append($holder);
The benefit is that all of this javascript can be cached for your page. It is loaded once and is capable of handling large amounts of links without having to worry about sending excessive html to the client.

Resources