Orchard CMS - Count specified items of a container part - asp.net-mvc

I have 'Association' content type with added Container Part. I also have "Company" and "Bearer" content types which have Containable Part. So Association contains some companies and bearers. I have query which returns me all associations. I'm trying to show items in grid (i have created custom layout provider and custom view for cell of grid). It's working, but when i'm trying to get items from Container part for each associatian - i don't get it. I mean i can get Container Part and even can get Items count , but can't get content items for companies and bearers. Also it will show me whole count of items in Container part. I need to get count of companies in Container part, exclude bearers count. How can i do it? Thanks!

The actual content items aren't stored on ContainerPart. Instead, each contained item's CommonPart has a Container value that links to the container. You need to use LINQ to locate all ContentItems that have CommonPart.Container equal to your container.
int containerId = containerItem.Id;
var containedList = _contentManager
.Query<CommonPart>()
.Join<CommonPartRecord>()
.Where(x => x.Container.Id == containerId)
.List();

Related

Accesing the content from immediate parent in Umbraco

I am trying to access content from my level 2 document type from a sub level in Umbraco. level 2 has multiple pages of same document type. I am using the code below
var rootByTraversing = Umbraco.AssignedContentItem.AncestorOrSelf(2);
var openingTimesByDescendants = rootByTraversing.Descendants().Where(f => f.DocumentTypeAlias == "PageLevel2").FirstOrDefault();
Is it possible for me to get the immediate parent node of this document type and not FirstOrDefault node?
I don't want to access the content through node id.
Not sure I follow entirely, especially why you don't want to access via node ID... Your current content item has a Path property in which all ancestors IDs are listed in a comma separated list. Couldn't you just split that string and select whichever level (like ancestors[2] for level 2, I'd guess) to get the ID of that node. Then you could go something like
var level2AncestorId = Umbraco.AssignedContentItem.Path.Split(',')[2];
var openingTimesByDescendants = rootByTraversing.Descendants().Where(f => f.Id == (int)level2AncestorId && f.DocumentTypeAlias == "PageLevel2").FirstOrDefault();
Which should only give you one node and it should be the direct ancestor. Right?

How to get HTML generated at runtime and control imagery based on host header and lookup table in .ASP.Net

There are 3 concepts each type of user loggin in. The imagery is controlled by a lookup table based on the host header. By this, I mean that there are several domain names that point to the same IP/web instance, and that instance serves up the content based on reading the host header.
What we are doing now by having all the HTML (not the code) stored in a table that is referenced by the host header lookup.
CAn somebody guide me on this requirement please? Thanks
There are probably better ways to deal with multi-tenancy but let's assume you can't change any of that. What you want is probably this
string domain = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
You could map this domain onto a customer Id and store this in the user's cookie and look up based on this, mapping as necessary. Or simply use domain directly for your table lookup.
Request.Url.Host
This will give you the domain name such as: "www.example.com". I would recommend to store records including "view name" and "domain". Then, on your action, I would imagine something like this:
var record = db.HostRecords.Where(r => r.Domain == Request.Url.Host).FirstOrDefault();
var view = "Page.cshtml";
if (record != null){
view = record.ViewName;
}
return View(view);

Divide a user interface into asp.net mvc views

I have a CSS #MainDiv containing a #TreeDiv on the left side and a #DataGridDiv on the right side.
The TreeDiv contains a Javascript Treeview with Department objects and the DataGridDiv
contains a Datagrid with Employee objects.
Changing the selection of a Department in the Treeview should change also the related employee objects in the DataGrid.
I have setup a DepartmentController. Both controls should be able to recieve data via ajax
independently from each other.
1.) What kind of object should my Index method return to display this aggregated data in the view?
2.) How should I divide my controls into what sort of views?
2.) How should I divide my controls into what sort of views?
Create a view with #MainDiv, #TreeDiv and #DatagridDiv. Let #TreeDiv host your tree control (You already know this). Create a partial view to display the datagrid with employee objects. Let #DatagridDiv host this partial view.
Now when a department is selected in tree control, you can make an ajax call to a controller method which accepts the department and returns the partial view containing the employee data. Update the #DatagridDiv with returned data.
Alternatively if you are comfortable with Json, your controller method could return the employee data in Json format (instead of partial view) and You can populate this into an html table inside #datagridDiv using javascript/jquery.
1.) What kind of object should my Index method return to display this aggregated data in the view?
In the Index method you can return your view which contains all 3 Divs and #TreeDiv populated with tree control. On the client side when page loads you can identify the selected department to make an ajax call and update #datagridDiv. This approach will have a it lag on the clientside, however you can use that to display some animation indicating the page is loading/div is updating.
If you dont want to add this lag period, identify the department that will be selected when tree view is loaded and populate the partial view for that department, add this to your #datagridDiv on the server side and deliver.

ASP.NET MVC - Passing Grouped data into a View

I've got a LINQ to SQL object, and I want to group the selected data and then pass it into a view. What's the correct way of doing this? I'm sure I need to group the data when I select it rather than grouping it in the view, as this will result in about 200 rather 50000 rows I need to pass into my view. Are there any good examples of this online that anyone has seen?
Cheers
MH
-----edit-----
I want a bit of both:-
for example, my data object has (amongst others) 2 properties I want to extract, and group on, ItemDetail.ItemID and ItemDetail.Label - it is a set of those I want to pass into my view. My data factory returns a IQueryable which will contain (in live) about 100 records for each ItemID/Label combination, and thus I want to group this in my view so that it only shows 1 row per ItemID/Label combination.
Also, how do I type my View - I have tried passing in something like the var xxx = ...; return View(xxx); but I'm not sure how to strongly type (if I can) the view properly. I can probably boj this and get it working, but I wanted to do this correctly.
----edit 2----
I've just got a bit further on this.
using the var IQueryable itemDetList
itemDetList = itemDetList.OrderBy(i => i.ItemID).GroupBy(i => i.ItemID).Select(i => i.First());
produces a grouped list, with 1 row per ItemID, and preserves the object typing so that I can pass it into a strongly-typed view - is that the correct way of manipulating the data? How can I put another layer of grouping so that it groups by .Label within each .ItemID group?
You may want to abstract the model you pass to your view from the LINQ 2 SQL objects; check out the View Model Pattern. If this means you find yourself potentially writing lots of code to map properties from LINQ 2 SQL objects to your View Model objects then consider using AutoMapper.
Well, then group the data and pass it onto your View from your Controller...
public ViewResult Foo()
{
var data = this.GetGroupedData();
return this.View(data);
}
private IEnumerable<Bar> GetGroupedData()
{
return from x in GetData()
group x by x.Baz into g
select new Bar(g.Key);
}
I would define Presentation-Models that represent your groups in your View. Fill the Presentation-Models with LINQ and pass them to your View.
With Presentation-Models you have strongly typed data to display in your view.

asp.net-mvc - how do i create a view to show all unapproved users and allow them to be approved

i have this code in my membership service class (taken from the asp.net-mvc sample app)
public MembershipUserCollection GetUnapprovedUsers()
{
MembershipUserCollection users = Membership.GetAllUsers();
MembershipUserCollection unapprovedUsers = new MembershipUserCollection();
foreach (MembershipUser u in users)
{
if (!u.IsApproved)
{
unapprovedUsers.Add(u);
}
}
return unapprovedUsers;
}
i now need a view to show this list of information and allow someone to approve them which will go back to the controller and set the IsApproved property to true.
Create a view which will generate a form containing label and checkbox for each member of the collection. You need to be able to get from the id of the checkbox to the user.
In the HTTP.POST Action method, iterate through the submitted fields looking for set checkboxes, when you find one set the corresponding user to approved.
Obviously the form can display arbitrary details for each user.
To use the inbuilt control helpers takes a bit more effort because you don't have a fixed size model to work with. To achieve something similar I:
Used a non-strongly typed view
populated ViewData["ids"] with IEnumerable<IdType> (which the view would loop over)
For each entry populated ViewData["field" + id] for each field I was displaying in the entity
In the view looped over the ids using ViewData["ids"] to call the HTML helpers with the id of the field.
(That was V1, in V2 I used model state so I could use the inbuilt validation error display support, but that doesn't really apply if you just want to select users.)
The POST processing was similar, repopulating the id list from the database and the looking up in the passed FormCollection.

Resources