MVC 5, show data by group in Index page - asp.net

I'm new in MVC5.
I have 2 table, tblClient & tblBranch.
The Data Structure are:
tblClient
ID | Name
---------
1 | BCS
2 | LBBI
3 | CARB
tblBranch
ID | cID | BranchName
---------------------
1 | 1 | Savar
2 | 1 | Nobinogor
3 | 2 | Sawrapara
4 | 1 | Mirpur
5 | 3 | Motijheel
6 | 2 | Dhanmondi
7 | 1 | Kazipara
Now I need to show data in index page as like:
BCS
sL | Branch
-----------
1 | Savar
2 | Nobinogor
3 | Mirpur
4 | Kazipara
LBBI
sL | Branch
-----------
1 | Sawrapara
2 | Dhanmondi
CARB
sL | Branch
-----------
1 | Motijheel
How can I do this ?

Write a model class for your index view.
public class MyModel
{
public string GroupName;
public List<string> GroupItems;
}
Specify your model for your view:
#model IEnumurable<MyProject.MyModel>
#foreach (var item in Model)
{
<h2>#item.GroupName</h2>
<ol>
#foreach (var it in item.GroupItems)
{
<li>#it</li>
}
</ol>
}
And finally in the index action of your controller fill the model and pass it to the view:
public ActionResult Index()
{
var model = new List<MyModel>();
var item = new MyModel();
item.GroupName = "Hello World";
item.GroupItems = new List<string>() { "item1", "item2" };
model.Add(item);
return View(model);
}

You can wrap every tblClient in new Object:
tblClient has name and contains List of tblBranch
public Class Client
{
public string clientName {get;set;}
public List<Branch> branches {get;set;}
}
public Class Branch
{
public string branchName
}
and retriere List<Client> and show them to the UI

There is no need in doing something unusual. Especially, groupping by.
You can simply iterate through items and output it.
In ASP.NET Razor:
Dictionary<int, string> clients = // get dictionary from DB like {{1, "BCS"}, {2, "LBBI"} ...}};
List<BranchEntity> branches = // get list of Branch entities just like in DB;
// You can pass these values as ViewBag or together as a Model
#{
foreach (var client in clients)
{
var branchesForClients = branches.Where(x => x.cID == client.Key).ToArray();
<h3>#client.Value</h3>
<table>
for (int i = 0; i < branchesForClients.Length; i++)
{
<tr>
<td>#(i + 1)</td>
<td>#branchesForClients[i].BranchName</td>
</tr>
}
</table>
}
}
Here is an entity:
public class BranchEntity
{
public int Id { get; set; }
public int cId { get; set; }
public string BranchName { get; set; }
}

Related

linq query issues multiple tables

Here is what I have guys/girls...I have 3 tables as follows (only included important columns):
ITEMS:
ItemId Name ItemLocationId
20 Portal Orange 12
21 Portal Blue 13
ITEMLOCATIONS:
ItemLocationid ItemId CreateDate LocationIf
13 21 3/26/2017 2:19:15 AM 5
14 20 3/27/2017 6:25:45 PM 6
15 21 3/31/2017 12:17:25 AM 6
16 21 3/31/2017 12:18:42 AM 5
17 21 3/31/2017 12:20:23 AM 6
LOCATIONS
LocationId Name
5 Storage Room
6 Boss Room
My issue lies in the itemlocations table...I only need the most recent ItemLocation in the table...The others are for historical value...here is the query I am running now
Dim i = From r In mydb.Items
Join il In mydb.ItemLocations On r.ItemLocationId Equals il.ItemLocationId
Join l In mydb.Locations On il.LocationId Equals l.LocationId
Where r.CompanyId = UserPro.CompanyId
Select r.ItemId, r.Name, Location = l.Name
this is returning the first itemlocation in the table for that item...how do I get only the one with then most recent
did not have database available so sketched answer for you in LinqPad.
You need to select by date:
void Main()
{
List<Item> Items = new List<Item> { new Item { ItemID=20, ItemLocationID=12, Name="Portal Orqange"},
new Item{ ItemID=21, ItemLocationID=13, Name="Portal Blue"}};
List<ItemLocation> ItemLocations = new List<ItemLocation> {
new ItemLocation {ItemLocationID=13, ItemId=21, CreateDate=DateTime.Parse("3/26/2017 2:19:15 AM"), LocationId=5},
new ItemLocation {ItemLocationID=14, ItemId=20, CreateDate=DateTime.Parse("3/27/2017 6:25:45 PM"), LocationId=6},
new ItemLocation {ItemLocationID=15, ItemId=21, CreateDate=DateTime.Parse("3/31/2017 12:17:25 AM"), LocationId=6},
new ItemLocation {ItemLocationID=16, ItemId=21, CreateDate=DateTime.Parse("3/31/2017 12:18:42 AM"), LocationId=5},
new ItemLocation {ItemLocationID=17, ItemId=21, CreateDate=DateTime.Parse("3/31/2017 12:20:23 AM"), LocationId=6},
};
List<Location> Locations = new List<Location> { new Location { LocationID=5, Name="Storage Room"},
new Location { LocationID=6, Name="Boss Room"}
};
Items.Join(ItemLocations, i => i.ItemID, il => il.ItemId, (i, il) => new { i, il }).OrderByDescending(i =>i.il.CreateDate )
.Join(Locations, iil => iil.il.LocationId, il=>il.LocationID, (lc, c) => new {lc,c}).FirstOrDefault()
.Dump();
}
// Define other methods and classes here
public class Item
{
public int ItemID { get; set; }
public string Name { get; set; }
public int ItemLocationID { get; set;}
}
public class ItemLocation
{
public int ItemLocationID { get; set; }
public int ItemId { get; set; }
public DateTime CreateDate { get; set; }
public int LocationId { get; set;}
}
public class Location
{
public int LocationID { get; set; }
public string Name { get; set;}
}
When ready to code, just replace Dump() which is LinqPad specific to Select.
Result as follows:
What about this one
Dim groupQuery=from il mydb.ItemLocations
.group il by il.ItemId into gl
select gl.orderByDescending(g=>g.CreateDate).First();
Dim i = From r In mydb.Items
Join il In groupQuery On r.ItemLocationId Equals il.ItemLocationId
Join l In mydb.Locations On il.LocationId Equals l.LocationId
Where r.CompanyId = UserPro.CompanyId
Select r.ItemId, r.Name, Location = l.Name

Bind a list returned from a LINQ query to Gridview

I have a linq query something like this
var Query = from c in table
where (some condition)
select new {
Name = c.Name,
courses = // this returns a list,
};
How do I bind this to gridview so that the result is like this
name1 course1
name1 course2
name1 course3
name2 course1
name2 course2
Any ideas?
try below
gridview.DataSource = Query.ToList().Select(a => a.courses
.Select(c => new { Name = a.Name, Course = c }))
.SelectMany(p=>p).ToList();
gridview.DataBind();
If you want to return this list from method then create class as below
public class MyClass
{
public string Name { get; set; }
public string Course { get; set; }
}
now you can return list as
public List<MyClass> MyMethod()
{
var Query = from c in table
where (some condition)
select new {
Name = c.Name,
courses = // this returns a list,
};
return Query.ToList().Select(a => a.courses
.Select(c => new MyClass{ Name = a.Name, Course = c }))
.SelectMany(p=>p).ToList();
}

Recursivity with panels in ASP.NET

I have a database with a table called "categories". The table has 3 columns:
1. ID
2. Name
3. ParentID.
id | Name |ParentID
_____________________
1 | John | 0
2 | Charlie | 1
3 | Vasily | 1
4 | David | 2
5 | Edward | 3
So John is the parent for Charlie and Vasily, David is the child of Charlie and Edward is the child of Vasily.
The question is:
How can i create and populate some panels for each category, considering that the child panels must be inside their parent panels? The question is allmost the same as this one here however i realy realy need it to be with panels instead of treeview.
Thank You.
you should change the code as it is
public class MyObject
{
public int Id;
public int ParentId;
public string Name;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
List<MyObject> list = new List<MyObject>();
list.Add(new MyObject() { Id = 1, Name = "Alice", ParentId = 0 });
list.Add(new MyObject() { Id = 2, Name = "Bob", ParentId = 1 });
list.Add(new MyObject() { Id = 3, Name = "Charlie", ParentId = 1 });
list.Add(new MyObject() { Id = 4, Name = "David", ParentId = 2 });
BindTree(list, null);
}
}
private void BindTree(IEnumerable<MyObject> list, Panel parentPanel)
{
var nodes = list.Where(x => parentPanel == null ? x.ParentId == 0 : x.ParentId == int.Parse(parentPanel.ID));
foreach (var node in nodes) {
Panel newPanel = new Panel() { ID = node.Id.ToString() };
if (parentPanel == null) {
Panel1.Controls.Add(newPanel);
} else {
parentPanel.Controls.Add(newPanel);
}
BindTree(list, newPanel);
}
}

Having trouble with display colums from 4 tables MVC 3

I have a model as below and want to display items from 4 different table.
public class AuctionViewer
{
public int AuctionId { get; set; }
public string ProductName { get; set; }
public int Price { get; set; }
public DateTime startTime { get; set; }
public DateTime EndTime { get; set; }
public string Category { get; set; }
public int ExpectedHit { get; set; }
public string Status { get; set; }
}
Here is my controller code below.
public ActionResult Index()
{
MightyMouseContainer ctx = new MightyMouseContainer();
var NewList = new AuctionViewer
{
from CT in ctx.Categories
join PD in ctx.Items on CT.Category_ID equals PD.Category_ID
join AU in ctx.Auction_Schedule on PD.Item_ID equals AU.Item_ID
join ST in ctx.Status on AU.Status_ID equals ST.Status1
orderby AU.Auction_ID
select new
{
AuctionId = AU.Auction_ID,
ProductName = PD.Item_name,
Price= PD.Item_Value,
startTime = AU.Start_Time,
EndTime = AU.End_Time,
Category = CT.Category_Description,
Status = ST.Description
};
}
return View(NewList);
}
I wonder why is giving errors. please advise. I have been on this for quite a while and realized that I need some help to move on. I will appreciate prompt response in other to move forward. Thanks
Assuming your view accepts a model of IEnumerable<AuctionViewer>, you should change the select to return the strongly typed collection, e.g.:
var NewList = from CT in ctx.Categories
join PD in ctx.Items on CT.Category_ID equals PD.Category_ID
join AU in ctx.Auction_Schedule on PD.Item_ID equals AU.Item_ID
join ST in ctx.Status on AU.Status_ID equals ST.Status1
orderby AU.Auction_ID
select new AuctionViewer
{
AuctionId = AU.Auction_ID,
ProductName = PD.Item_name,
Price= PD.Item_Value,
startTime = AU.Start_Time,
EndTime = AU.End_Time,
Category = CT.Category_Description,
Status = ST.Description
};
return View(NewList);
Edit From your second error message, it seems that the view expects a List<AuctionViewer>. You can change the controller method to materialize the IQueryable as a List as follows:
var NewList = (from CT in ctx.Categories
join PD in ctx.Items on CT.Category_ID equals PD.Category_ID
join AU in ctx.Auction_Schedule on PD.Item_ID equals AU.Item_ID
join ST in ctx.Status on AU.Status_ID equals ST.Status1
orderby AU.Auction_ID
select new AuctionViewer
{
AuctionId = AU.Auction_ID,
ProductName = PD.Item_name,
Price= PD.Item_Value,
startTime = AU.Start_Time,
EndTime = AU.End_Time,
Category = CT.Category_Description,
Status = ST.Description
}).ToList();
return View(NewList);

How to add data,which is in a gridview column in asp.net

i am using asp.net.In my gridview control i have a column called "TotalAmount".The total amount in this column is to be calculated.How can i do this?
Like this:-
Col3(TotalAmount)
30
40
30
Total Amount=30+40+30=100
How can i calculate this 100?This is my question.
I gather you are toalling on a specific row (since you are mentioning it as a total column), i.e.
Val1 | Val2 | Val3 | Total
--------------------------
10 | 20 | 30 | 60
5 | 1 | 1 | 7
Then depending on what your datasource is you should either calculate the TotalAmount in your SQL statement or as a property (preferred) in the classes/objects you are binding. Your TotalAmount column is then basically a BoundColumn/Field for that field/property.
Doing the sum is "business logic" and is as such not a job for the GridView.
Class way:
public class MyData
{
public int Val1 { get; set; }
public int Val2 { get; set; }
public int Val3 { get; set; }
public int TotalAmount { get { return this.Val1 + this.Val2 + this.Val3; } }
}
or whatever your logic is

Resources