I currently have a navigation bar which is dynamically created from a MS SQL Database, but I would like to add a second sub layer. Below is what I have so far for the current working single level navigation bar.
Navigation
public partial class Navigation
{
public int Id { get; set; }
public string Title { get; set; }
public Nullable<int> Position { get; set; }
public bool Main { get; set; }
public string Action { get; set; }
public string Controller { get; set; }
}
HomeController
[ChildActionOnly]
public ActionResult Navigation()
{
var navigationModel = (from m in db.Navigations where (m.Main == true) orderby m.Position select m);
return View(navigationModel);
}
Navigation
#{ Layout = null; }
#model IEnumerable<WebApplication1.Models.Navigation>
<ul class="nav sf-menu clearfix">
#foreach (var item in Model)
{
#Html.MenuLink(item.Title, item.Action, item.Controller)
}
</ul>
The second tier needs to link to the Contents table, the relationship is db.Navigations.Id = db.Contents.NavigationId.
Content
public partial class Content
{
public int Id { get; set; }
public Nullable<int> NavigationId { get; set; }
public string Title { get; set; }
public string Content1 { get; set; }
public Nullable<int> Position { get; set; }
public string Image { get; set; }
public string Sub { get; set; }
public Nullable<bool> Active { get; set; }
public string Url { get; set; }
public string Summary { get; set; }
}
Could someone please provide a tutorial or example code of how I can bind child data to the parent dataset and then in the View, how to check if a parent holds child records and if so loop through them.
Any help would be much appreciated :-)
I added a new class called NavigationViewModel to declare the tables I wish to use and modified the HomeController to use NavigationViewModel for querying the respective tables.
NavigationViewModel
namespace WebApplication1.Models
{
public class NavigationViewModel
{
public List<Navigation> Navigation { get; set; }
public List<Content> Content { get; set; }
}
}
HomeController
[ChildActionOnly]
public ActionResult Navigation()
{
var navigationModel = new NavigationViewModel();
navigationModel.Navigation = (from m in db.Navigations where (m.Main == true) orderby m.Position select m).ToList();
navigationModel.Content = (from n in db.Contents where (n.Active == true) orderby n.Position select n).ToList();
return View(navigationModel);
}
Navigation
#model WebApplication1.Models.NavigationViewModel
<ul class="nav sf-menu clearfix">
#foreach (var navigation in Model.Navigation)
{
int records = Model.Content.Count(c => c.NavigationId == navigation.Id);
if (records > 0)
{
#Html.SubMenuLink(navigation.Title, navigation.Action, navigation.Controller)
#Html.Raw("<ul>")
foreach (var content in Model.Content.Where(c => c.NavigationId == navigation.Id))
{
if (string.IsNullOrEmpty(content.Url))
{
if (string.IsNullOrEmpty(content.Content1))
{
}
else
{
#Html.MenuLink(content.Title, "Home/Article/" + content.Id + "/" + ToFriendlyUrl(content.Title), "Home");
}
}
else
{
#Html.MenuLink(content.Title, content.Url, "Home");
}
}
#Html.Raw("</ul>")
#Html.Raw("</li>")
}
else
{
#Html.MenuLink(navigation.Title, navigation.Action, navigation.Controller)
}
}
</ul>
I'm not sure this is the most elegant or efficient way to achieve the end goal, any suggestions to make the code more efficient I am happy to listen.
Related
I'm using ASP.NET Core 2.2. I have 2 models and a viewmodel which injects data to a view. I want to order results based on their productType. Let's make it clear
This is Product model
public class Product
{
public int ProductID { get; set; }
public int ProductName { get; set; }
public string ProductImage { get; set; }
public int ProductTypeID { get; set; }
[ForeignKey("ProductTypeID")]
public virtual ProductType ProductType{ get; set; }
}
This is ProductType model
public class ProductType
{
public int ProductTypeID { get; set; }
public int ProductTypeName { get; set; }
public string ProductTypeImage { get; set; }
public string ProductTypeDescription { get; set;
}
And finally this is DishesViewModel
public class DishesVM
{
public IEnumerable<ProductType> ProductType { get; set; }
public IEnumerable<Product> Product { get; set; }
}
In MyController I get data from DB then with automapper, map them to DishViewModel
public class HomeController : Controller
{
public IActionResult Dishes()
{
var productTypes= _context.ProductType.OrderBy(p =>p.ProductTypeID).ToList();
var products= _context.Products.OrderBy(p => p.ProductID).ToList();
var DishesVM = new DishesVM();
DishesVM.ProductType = _mapper.Map<IEnumerable<ProductType>>(productTypes);
DishesVM.Product = _mapper.Map<IEnumerable<Product>>(products);
}
}
Now in Dishes View I can have nested foreach
#model DishesViewModel
<div>
foreach(var pt in Model.ProductType)
{
<h1>pt.ProductTypeName</h1>
foreach(var p in Model.Product)
{
p.ProductName
}
}
</div>
This works fine but the only problem it has, is it returns all products. but I want each Product Category has its Products In front of its header. This is visual representation of what I want and what I have now.
This is what I want
But this is what I have
You have to filter your products by product type in each iteration. At the moment you just display all products for each product type:
<div>
foreach(var type in Model.ProductType)
{
//products which belong to the particular type
var productsForType = Model.Product.Where(x => x.ProductTypeID == type.ProductTypeID);
<h1>pt.ProductTypeName</h1>
foreach(var product in productsForType)
{
product.ProductName
}
}
</div>
This will give you a jump on the linq statement:
var list = productTypes.Where(x => x.ProductTypeID == 1).Select(x => new Product()
{
ProductImage = x.ProductTypeImage,
}).ToList();
You will need to decide on what to put into the where clause, I'm using ProductTypeID
I am very new to .NET MVC and trying to learn MVC. I know that I am doing total wrong here, so I need your help. What I try to do is listing a set of 10 companies, then for each of those company listing the contacts based on the companyID. Please assume that the Entitites and DbContext are set properly, just the problem is between Controller and View is where I couldn't figure out how to:
Here is my Model:
namespace ERP.Models
{
[Table("ERP_Company")]
public class ERP_Company
{
[Key]
public int CompanyID { get; set; }
public string Name { get; set; }
}
[Table("ERP_CompanyContact")]
public class ERP_Contact
{
[Key]
public int ContactID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int CompanyID { get; set; }
}
}
The methods for getting Company and Contact list from the database:
namespace ERP.Models
{
public class Method1
{
private ERPEntities db = new ERPEntities();
public List<ERP_Company> getCompanyList()
{
List<ERP_Company> companyList = (
from c in db.ERP_Company
where c.Name.Contains("Network")
select c).Take(10).ToList();
return companyList;
}
public List<ERP_Contact> getContactList(int CompanyID)
{
List<ERP_Contact> contactList = (
from cc in db.ERP_CompanyContact
where cc.CompanyID == CompanyID
select cc).Take(50).ToList();
return contactList;
}
}
}
Here is my controller where I am doing wrong:
namespace ERP.Controllers
{
public class Test1Controller : Controller
{
//private ERPEntities db = new ERPEntities();
Method1 _repository = new Method1();
public ActionResult Index()
{
ViewData["Company"] = _repository.getCompanyList();
ViewData["Contact"] = _repository.getContactList(CompanyID); // <-- Incorrect Here, but just to show that I want to pass the CompanyID
return View();
}
}
}
Lastly, the View which I want to list the Company, then query all contacts based on CompanyID and list them.
<ul>
#foreach (var item in ViewData["Company"] as List <ERP.Models.ERP_Company>
)
{
<li>#item.CompanyID | #item.Name</li>
<!-- Here is an EXAMPLE that I want to QUERY the Contact recordset and list all the contacts based on the CompanyID -->
<ul>
#for (var i = 0; i < 5; i++)
{<li>Contact #i</li>}
</ul>
}
</ul>
Is it possible to loop through the Contact model (or recordset) within a loop? How can I accomplish this?
Thanks in advance,
Here is how I would implement your case, not in a best way but in a simple way.
Entities:
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Contact> Contacts { get; set; }
}
public class Contact
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int CompanyId { get; set; }
}
Service:
public class CompanyService
{
public List<Company> getCompanyList()
{
using (ERPEntities db = new ERPEntities())
{
return db.Companies
.Include("Contacts")
.Where(e => e.Name.Contains("Network"))
.Take(10)
.ToList();
}
}
}
Controller:
public HomeController(CompanyService companyService)
{
this.companyService = companyService;
}
public ActionResult Index()
{
List<Company> companies = this.companyService.getCompanyList();
return View(companies);
}
View:
<ul>
#foreach (var company in Model)
{
<li>#company.Id | #company.Name</li>
if (company.Contacts.Count > 0)
{
<ul>
#foreach (var contact in company.Contacts)
{
<li>#contact.FirstName</li>
}
</ul>
}
}
</ul>
In other hand, judging from your implementation, I feel like you may need to work more on fundamental skills like data structure, C#/OOP fundamental then ASP.NET MVC in respective order.
I just followed this guide to install and set up AutoMapper, and I think I got it correctly set up. But when I try to map from IEnumerable<ProductCategory> to IEnumerable<ViewModelProductCategory>, like this:
var categories = _context.ProductCategories.Include(e => e.Children).ToList();
var topLevelCategories = categories.Where(e => e.ParentId == null);
var VMCategory = _mapper
.Map<IEnumerable<ProductCategory>,
IEnumerable<ViewModelProductCategory>>(topLevelCategories);
return View(VMCategory);
(Thank you to #Anton Gorbunov, for pointing out that I had my objects swapped!)
... my view complains about receiving the wrong model. I'm pretty sure it's because of the children categories not being mapped to a sub-list, so in the view, item.Children is not properly set up as a viewmodel.
How can I map a list of lists?
This is my Index-view:
#model IEnumerable<MyStore.Models.ViewModels.ViewModelProductCategory>
<ul>
#Html.Partial("_CategoryRecursive", Model)
</ul>
... and this is _CategoryRecursive.cshtml:
#model IEnumerable<MyStore.Models.ViewModels.ViewModelProductCategory>
<ul style="list-style:none;padding-left:0px;">
#if (Model != null)
{
foreach (var item in Model)
{
<li style="margin:8px 0px 0px 0px;">
<ul>
#Html.Partial("_CategoryRecursive.cshtml", item.Children)
</ul>
</li>
}
}
</ul>
Mapping profile:
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<ProductCategory, ViewModelProductCategory>();
CreateMap<ViewModelProductCategory, ProductCategory>();
CreateMap<Product, ViewModelProduct>();
CreateMap<ViewModelProduct, Product>();
}
}
Entity model:
public class ProductCategory
{
public int Id { get; set; }
public int SortOrder { get; set; }
public string Title { get; set; }
[ForeignKey(nameof(ParentCategory))]
public int? ParentId { get; set; }
public ProductCategory ParentCategory { get; set; } //nav.prop to parent
public ICollection<ProductCategory> Children { get; set; } //nav. prop to children
public List<ProductInCategory> ProductInCategory { get; set; }
}
Viewmodel:
public class ViewModelProductCategory
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Title { get; set; }
public int SortOrder { get; set; }
public string ProductCountInfo
{
get
{
return Products != null && Products.Any() ? Products.Count().ToString() : "0";
}
}
public ProductCategory ParentCategory { get; set; } // Nav.prop. to parent
public IEnumerable<ProductCategory> Children { get; set; } // Nav.prop. to children
public List<ViewModelProduct> Products { get; set; } // Products in this category
public List<ViewModelProduct> OrphanProducts { get; set; } // Products with no references in ProductInCategory
}
Problem here:
var VMCategory = _mapper.Map<IEnumerable<ViewModelProductCategory>, IEnumerable<ProductCategory>>(topLevelCategories);
you should change code to
var VMCategory = _mapper.Map<IEnumerable<ProductCategory>, IEnumerable<ViewModelProductCategory>>(topLevelCategories);
Description:
TDestination = Map<TSource,TDestination>(TSource sourceObject), but you swapped the types TSource and TDestination inside brackets <,>
I used scaffolding to create the Index, Details, Create, Edit and Delete views and the controller. I have two view models (Parent / Child) relation. In my Index view I want to display the list of Teams as well as some information on the players (Parent / child). For example I want to display in the Index view the teams with the players count per team and last players that was modified. I am not sure where to begin.
Example:
(Team) Red -- (Last Modified) 01/02/2015 -- (Number Players) 10 and so on.
Team ViewModel
public class TeamVM
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime? LastActivity { get; set; }
public string NumberPlayers { get; set; }
public IList<PLayerVM> PlayerVM { get; set; }
}
Player ViewModel
public class PlayerVM
{
public int ID { get; set; }
public int TeamID { get; set; }
public string PlayerInfo { get; set; }
public DateTime? CreateDate { get; set; }
}
Other ViewModel
public class TeamViewModel
{
public List<Team> Teams{ get; set; }
}
Controller
public ActionResult Index()
{
TeamViewModelviewModel = new TeamViewModel();
viewModel.Teams= db.Teams.ToList();
return View(viewModel);
}
db.Products.ToList()?? I assume that is where you mean db.Teams.ToList()?
You are using viewmodels, so you should map the db data to your viewmodels first:
public ActionResult Index()
{
var teams = db
.Teams
.Include("Players") // Assuming your Team entity has a collection of Players
.SelectMany(t => new TeamVM {
ID = t.ID,
// etc..
})
.ToList();
return View(new TeamViewModel { Teams = teams });
}
model:
public class TeamVM
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime? LastActivity { get; set; }
public IList<PLayerVM> PlayerVM { get; set; }
public int NumberPlayers {
get { return PlayerVM.Count(); }
}
}
Then in your view:
#model MyProject.Models.TeamViewModel
<table>
#foreach(var team in Model.Teams.ToList()) {
<tr>
<td>#team.Name</td> // Name
<td>#team.NumberPlayers</td> // Playercount
<td>#team.PlayerVM.Max(p => p.LastActivity).LastActivity</td> // Last edited
</tr>
}
</table>
Hi im making a project using asp.net mvc 4.0 , with entity framework and linq...
So i have two classes.
Cliente Class:
namespace LicenciamentoMVC.Models
{
public class Cliente
{
[Key]
public int IDCliente { get; set; }
public string Nome { get; set; }
public string Morada { get; set; }
public string CPostal { get; set; }
public string Localidade { get; set; }
public string Freguesia { get; set; }
public string Conselho { get; set; }
public string Distrito { get; set; }
public string Pais { get; set; }
public string Telefone { get; set; }
public string Telemovel { get; set; }
public string Fax { get; set; }
public string Email { get; set; }
public string Nif { get; set; }
public string WWW { get; set; }
public string Observacoes { get; set; }
public int IDP { get; set; }
public int IDU { get; set; }
public DateTime TStamp { get; set; }
public int Rem { get; set; }
public String TipoCliente { get; set; }
}
public class ClienteModel
{
private static Cliente entity;
public static IQueryable<Cliente> GetListaClientes()
{
MvcApplication1Context db = new MvcApplication1Context();
var customers = from c in db.Clientes
orderby c.IDCliente descending
where c.Rem==0
select c;
return customers.AsQueryable<Cliente>();
}
}
}
and here is my Processo Class
public class Processo
{
[Key]
public int IDProcesso { get; set; }
public int IDCliente { get; set; }
public DateTime DataInserido { get; set; }
public string NumeroFactura { get; set; }
public DateTime DataFactura { get; set; }
public string Estado { get; set; }
public int IDU { get; set; }
public int Rem { get; set; }
public DateTime TStamp { get; set; }
}
Lets say i want to view the the IDprocesso and DataInserido from class Processo, and Nome from class Cliente.
My database is composed of two tables Clientes and Processos.
My dbContent Class:
public class MvcApplication1Context:DbContext
{
public MvcApplication1Context()
: base("name=MvcApplication1Context")
{
}
//protected override void OnModelCreating(DbModelBuilder modelBuilder)
//{
// modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//}
public DbSet<Cliente> Clientes { get; set; }
public DbSet<Processo> Processos { get; set; }
}
The method GetListClientes is an iqueryable because i am using Igniteui components, in this case igGrid and i followed a tutorial that i found in the infragistics site, if there is a better way to do this, please tell me..
Back to the topic..
I didnt declare the foreign keys because of problem with contrains..
Should i create a new class with the fields that i want to see , or is there another way.
if i go with the method of create a new class for showing the data i should do something like this inside that class:
public class ProcessoClienteModel
{
private static ProcessoCliente entity;
public static IQueryable<ProcessoCliente> GetListaProcessos()
{
MvcApplication1Context db = new MvcApplication1Context();
var processos = from p in db.Processos
from c in db.Clientes
orderby p.IDProcesso descending
where p.IDCliente == c.IDCliente
where p.Rem == 0
select new { p.processoID,p.DataInserido,c.Nome} as IQueryable<ClienteModel>;
return processos;
return processos.AsQueryable<ProcessoCliente>();
}
}
Or is there a better way for doing this...
I am going through topics on internet trying to find a way..
Thanks for the help..
So i have been trying to find a way and the thing that i am trying to make is a viewmodel class, in this view model i will have the fields that i want to display, the Processo id, Data Inserido and the name of the cliente..and this class wil fetch value through linq to my domain class, in this case processo and cliente...
Am i going in the right direction???
Using the tip that Bjorn Vdkerckhove gave me, thanks, i have done this..
Create a new folder called viewmodel, and inside create a new class called ProcessoCliente.cs
The code of this class:
namespace LicenciamentoMVC.ModelsView
{
public class ProcessoCliente
{
public int IDProcesso { get; set; }
public string NomeCliente { get; set; }
public DateTime DataInserido { get; set; }
}
public class ProcessoModel
{
private static ProcessoCliente entity;
public static IQueryable<ProcessoCliente> GetListaProcessosClientes()
{
MvcApplication1Context db = new MvcApplication1Context();
var processos = (from p in db.Processos
join c in db.Clientes on p.IDCliente equals c.IDCliente
orderby p.IDProcesso descending
select new ProcessoCliente { IDProcesso = p.IDProcesso, NomeCliente = c.Nome, DataInserido = p.DataInserido});
return processos.AsQueryable<ProcessoCliente>();
}
}
}
then i create the view on the index action of controller Processo
#model IEnumerable<LicenciamentoMVC.ModelsView.ProcessoCliente>
#using Infragistics.Web.Mvc
#{
ViewBag.Title = "Index";
}
#* render of ignite ui grid *#
#( Html.Infragistics().Grid<LicenciamentoMVC.ModelsView.ProcessoCliente>()
.Caption("Processos")
.ID("grid1")
.DefaultColumnWidth("200px")
.PrimaryKey("IDProcesso")
.Columns(column =>
{
column.For(x =>x.NomeCliente).DataType("string").HeaderText("Nome do Cliente").Width("60%");
column.For(x => x.DataInserido).DataType("DateTine").HeaderText("Data de Criação do Processo").Width("40%");
column.For(x => x.IDProcesso).DataType("int").Width("0%");
})
.Features(features =>
{
features.Paging().PageSize(20).PrevPageLabelText("Previous").NextPageLabelText("NEXT");
features.Sorting().Mode(SortingMode.Single).ColumnSettings(settings =>
{
settings.ColumnSetting().ColumnKey("NomeCliente").AllowSorting(true);
});
features.Selection().MultipleSelection(false).Mode(SelectionMode.Row);
features.Filtering().Mode(FilterMode.Simple);
features.Updating()
.EnableAddRow(false)
.EnableDeleteRow(true)
.EditMode(GridEditMode.None);
})
.DataSourceUrl(Url.Action("ListarProcessos"))
.UpdateUrl(Url.Action("DeleteProcessos"))
.AutofitLastColumn(false)
.Width("100%")
.AutoGenerateColumns(false)
.DataBind()
.Render()
)
And i have this action inside ProcessoController to load the grid..
[GridDataSourceAction]
public ActionResult ListarProcessos()
{
return View(LicenciamentoMVC.ModelsView.ProcessoModel.GetListaProcessosClientes());
}
It works, but i would like to know if this is the right way to go...or there is a better way to do it..
Thanks again,..
You don't have any real relation between the objects if i understand you correctly. It all depends on how much data you need to show.
If you want to show related data, you can do this on your view with razor:
#foreach(var client in Model.Clientes)
{
<h1>#client.Name</h1>
<ul>
<li>Model.Processo.FirstOrDefault(f=> f.IDCliente == client.IDCliente).DataInserido.toString("dd-MM-yyyy")</li>
<li>.... other properties...</li>
</ul>
}