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>
Related
I have developed a Site using asp net mvc 6, I have used enom as categories, now I need to get product count under each enom value,
Here is my enum look like,
namespace ecom.Data
{
public enum BookCategory
{
Action,
Comedy,
Drama,
Others
}
}
What I want is get products count under this enoms,
See here, Front view without product count
This is my Controller Looks like,
using ecom.Data.Services;
namespace ecom.Controllers;
public class HomeController : Controller
{
private readonly IBooksService _service;
public async Task <IActionResult> BookView(string slug)
{
var data = await _service.GetBookBySlugAsync(slug);
if(data == null) return View("NotFound");
return View("BookView", data);
}
This is model which i use as product model,
namespace ecom.Models
{
public class Book:IEntityBase
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Slug { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public string Image { get; set; }
public string PublishDate { get; set; }
//enom
public BookCategory BookCategory { get; set; }
//Relationships
public List<Writter_Book> Writter_Books { get; set; }
//Publisher
public int PublisherId { get; set; }
[ForeignKey("PublisherId")]
public Publisher Publisher { get; set; }
}
}
My cshtml view right now,
#foreach (var cat in Html.GetEnumSelectList<BookCategory>())
{
<li>#cat.Text <span>()</span></li>
}
I want to get product count to above inside foreach's span tag,
Anyone can help me with that??
You can use GroupBy and Count to achieve your goal.
Something like
myBooksDataSource
.GroupBy(book => book.BookCategory)
.Select(group => new { Category = group.Key, Count = group.Count() });
Which will output a collection of (BookCategory, Count).
{ BookCategory.Action, 20 }
{ BookCategory.Comedy, 204 }
You can create a class to hold that data if you need to pass it around.
public class BookCategoryCountData
{
public BookCategory Category { get; set; }
public int Count { get; set; }
}
and select using that object.
public IEnumerable<BookCategoryCountData> GetBookCountByCategory()
{
return _context.Books
.GroupBy(book => book.BookCategory)
.Select(group => new BookCategoryCountData() {
Category = group.Key,
Count = group.Count()
});
}
I'm new to asp.net and may be missing some simple solution. So basically i've got two tables Movie and Actor here are their models:
public class Movie
{
public Movie()
{
this.Actors = new HashSet<Actor>();
}
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Genre { get; set; }
[Required]
public DateTime ReleaseDate { get; set; }
[Required]
public DateTime AddDate { get; set; }
public virtual ICollection<Actor> Actors { get; set; }
}
public class Actor
{
public Actor()
{
this.Movies = new HashSet<Movie>();
}
public int id { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Movie> Movies { get; set; }
public DateTime BirthDate { get; set; }
}
I'm trying to load movie details and the list of actors from the movie using ViewModel:
public class MovieDetailsViewModel
{
public Movie Movie { get; set; }
public Actor Actor { get; set; }
}
And the controller for it is here:
private ApplicationDbContext _context;
public MoviesController()
{
_context = new ApplicationDbContext();
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
public ActionResult Index()
{
var movie = _context.Movies.ToList();
return View(movie);
}
public ActionResult Details(int id)
{
var movie = _context.Movies.SingleOrDefault(m => m.Id == id);
var actor = _context.Actors.SingleOrDefault(a => a.id == id);
var viewModel = new MovieDetailsViewModel
{
Movie = movie,
Actor = actor
};
if (movie == null)
return HttpNotFound();
return View(viewModel);
}
Action Details is the one that needs to be loading the data that I need.
View looks something like this:
#model Movie_Rentals.ViewModels.MovieDetailsViewModel
#{
ViewBag.Title = Model.Movie.Name;
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>#Model.Movie.Name</h2>
<ul>
<li>Genre: #Model.Movie.Genre</li>
<li>Release date: #Model.Movie.ReleaseDate.ToShortDateString()</li>
<li>Add date: #Model.Movie.AddDate.ToShortDateString()</li>
<li>Actors:#Model.Movie.Actors</li>
</ul>
And it displays Actors:System.Collections.Generic.HashSet`1[Movie_Rentals.Models.Actor] which I suppose it should, but I can't mannge to find a solution on how to make it to display the actual list of actors. Does anyone have any idea on my issue, I would appreciate any help.
P.S. I'm not a native english speaker, so sorry if my english is bad.
in your view you could try:
<li>Actors: #string.Join("," Model.Movie.Actors.Select(a => a.Name).ToList())</li>
In this way, you will show a list of actors' names associated with the movie you added to your view model.
But I would suggest you refactor your view model to include a List of actors' names, so you would remove the logic from your view. After that, your view should look like:
public class MovieDetailsViewModel
{
public Movie Movie { get; set; }
public List<string> ActorsNames { get; set; }
}
In your controller you should do:
public ActionResult Details(int id)
{
var movie = _context.Movies.SingleOrDefault(m => m.Id == id);
var viewModel = new MovieDetailsViewModel
{
Movie = movie,
ActorsNames = string.Join(",", movie.Actors.Select(a => a.Name).ToList())
};
if (movie == null)
return HttpNotFound();
return View(viewModel);
}
As you can see, I have removed the code where you get a single actor from the Actors DbSet because it isn't useful. Indeed, you want the full list of all actors that have appeared in the movie you get from the Movies DbSet, not just an actor that has the same id of the movie of which you have to load the details.
Also, be sure to have activated lazy loading to get the collection of actors associated with the movie, otherwise, you will get an empty collection.
Let me know if my suggestions will be useful.
You need to load the Movie with all Actors using
_context.Movies.Include(x => x.Actors).SingleOrDefault(m => m.Id == id);
Then in the View
#foreach (var x in Model.Movie.Actors)
{<li>x.Name</li>}
To allow for the Actors field to show, you can handle this in a few ways. The easiest solution would be to add a property that parses the Actors collection into a string. You could also write code directly into the View to loop over each actor in the collection.
Example:
public class Movie
{
public Movie()
{
this.Actors = new HashSet<Actor>();
}
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Genre { get; set; }
[Required]
public DateTime ReleaseDate { get; set; }
[Required]
public DateTime AddDate { get; set; }
public virtual ICollection<Actor> Actors { get; set; }
public string ActorsToString
{
get
{
string _resultSet = "";
if (Actors != null && Actors.Count > 0)
{
foreach (Actor _actor in Actors)
{
_resultSet += $"{_actor.Name}, ";
}
}
return _resultSet;
}
}
}
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
So i'm working with MVC 4 and i have a question. I have two classes, Schedule and Simulation(its to simulate a student to enter the classroom). I have a view that gives all the schedules that exists next to a link to simulate entrance (it's another view, another class). I would like to pass the id from the schedule that the person chooses to a attribute in the Simulation class.
Class simulation:
namespace GestorSalas.Models
{
public class Simulation
{
public virtual int SimulationId { get; set; }
public virtual int ScheduleId { get; set; }
public virtual string Utilizador { get; set; }
[DisplayName("Tipo de Utilizador")]
public virtual string TipoUtilizador { get; set; }
public virtual int Codigo { get; set; }
public virtual string Hora { get; set; }
public virtual bool Entrar { get; set; }
}
Class Schedule:
namespace GestorSalas.Models
{
public class Schedule
{
public virtual int ScheduleId { get; set; }
public virtual int DisciplinaId { get; set; }
public virtual int SalaId { get; set; }
[Required]
public virtual int Dia { get; set; }
[Required]
[DisplayName("Hora de Entrada")]
public virtual string HoraEntrada { get; set; }
[Required]
[DisplayName("Hora de Saida")]
public virtual string HoraSaida { get; set; }
public virtual Disciplina Disciplina { get; set; }
public virtual Sala Sala { get; set; }
}
This is the view: (what the user sees)
(Entrar na sala=link to the simulation create view, in this image is the schedule index view).
I would like to pass the id from the schedule in order to appear in the simulation form (or in the table after the creation, much like when we click details or edit and it takes the user id but i want the schedule id).
This is the code in the "Entrar em sala" link:
#Html.ActionLink("Entrar em sala", "Create", "Simulation", new {id = item.HorarioId }, null)
But it doesn't work. Any ideas on how can i do this?
EDIT: The controllers:
Simulation:
To create
//
// GET: /Simulacao/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Simulacao/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Simulation simulation)
{
if (ModelState.IsValid)
{
db.Simulacaos.Add(simulation);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(simulation);
}
And the schedule is just the index that is being used here:
public ActionResult Index()
{
var schedules= db.Schedules.Include(h => h.Disciplina).Include(h => h.Sala);
return View(schedules.ToList());
}
Your create method should accept the id as a parameter and use it as needed.
public ActionResult Create(int id)
{
// Id has the scheduleId
// to do : Do something with the Id passed in and return something
}
and in your index view, you need to pass the scheduleId as value of route param Id
#model List<Schedule>
<h2>Simulator</h2>
<table>
<tr><th>HoraEntrada </th><th>Dia </th><th></th></tr>
#foreach(var item in Model)
{
<tr>
<td>#item.HoraSaida</td>
<td>#item.Dia</td>
<td>#Html.ActionLink("Entrar em sala", "Create", "Simulation",
new {#id = item.ScheduleId}, null)
</td>
</tr>
}
</table>
I created this viewmodel:
public class PlayerViewModel
{
PlayerRepository repo = new PlayerRepository();
public Player Player { get; set; }
public int SelectedUserID { get; set; }
public SelectList Users { get; set; }
public PlayerViewModel()
{
Player = new Player();
}
public PlayerViewModel(int id)
{
Player = repo.Retrieve(id);
Users = new SelectList(repo.GetUsers());
SelectedUserID = 0;
}
}
this I have in view:
#Html.DropDownListFor(x => x.SelectedUserID, Model.Users)
#Html.ValidationMessageFor(x => x.SelectedUserID)
and this in controller:
[Authorize]
public ActionResult Upravit(int id)
{
var playerview = new PlayerViewModel(id);
return View(playerview);
}
[Authorize,HttpPost]
public ActionResult Upravit(int id, PlayerViewModel playerView)
{
if (ModelState.IsValid)
{
playerView.Player.User = usRepo.GetUserById(playerView.SelectedUserID);
repo.Save(playerView.Player);
return RedirectToAction("Podrobnosti", new { id = playerView.Player.PlayerID });
}
return View(playerView);
}
Now I have problem that " The field SelectedUserID must be a number." and I have in dropdownlist UserName. I modified this many times, I tried with Dictionary and other ways but everyway has some problem. So I want just ask for best way to add custom class User to class Player.
Player class:
public class Player
{
// pokud použijeme virtual a vlastností tak nám EF rozšíří o další možnosti jako lazy loading a další
[Key]
public int PlayerID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Surname { get; set; }
public string PhotoUrl { get; set; }
public string Post { get; set; }
public virtual Team Team { get; set; }
public virtual User User { get; set; }
// public int UserID { get; set; }
//public virtual ICollection<Article> Articles { get; set; }
// Here could be next things as number, ...
}
Thanks
Use this constructor instead:
http://msdn.microsoft.com/en-us/library/dd505286.aspx
public SelectList(
IEnumerable items,
string dataValueField,
string dataTextField
)
Something like this:
Users = new SelectList(repo.GetUsers(),"UserID", "UserName");