Comparing data from two models in one controller - c#

I have two models:
public class Resort
{
public int ID { get; set; }
public String Name { get; set; }
public int BlackDiamond { get; set; }
public int BlueSquare { get; set; }
public int GreenCircle { get; set; }
public int TerrainPark { get; set; }
}
And
public class Input
{
public int ID { get; set; }
public string Name { get; set; }
public bool GreenCircle { get; set; }
public bool BlueSquare { get; set; }
public bool BlackDiamond { get; set; }
public bool TerrainPark { get; set; }
}
The idea here is to create a Controller that will allow me to have access to data from both Models as my logic will basically allow me to search through all Resorts looking for the Resort with the highest number of whichever experience level is preferred. As an example, let's say that I prefer BlackDiamonds, so I would want to search for the Resort with the highest number of BlackDiamonds. So I will need to know which experience level is preferred and which Resort has the highest number of that preferred experience level.

You could define a view model aggregating those 2 models:
public class MyViewModel
{
public Input Input { get; set; }
public Resort Resort { get; set; }
}
Now the controller actions could take/pass this view model from/to the views which will be strongly typed to the view model. Now you will have all the necessary data.

Related

Whats is a viable approach for a Template Data Model consists of two lists?

At the moment im working on a project where you can create a template for a report. At this point im struggling between two viable approaches for the entity model and I'm not sure which way is from the architectural approach the way to go.
A template consists of the following attributes:
Id
Name
List of Materials
List of Services
For each material and service position I have to declare a count of how many is needed.
I have two approaches how I can build it:
Approach I:
public class Template
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<TemplatePosition> Positions { get; set; }
}
public class TemplatePosition
{
public int Id { get; set; }
public int Count { get; set; }
public Service Service { get; set; }
public Material Material { get; set; }
public Template Template { get; set; }
[NotMapped]
public TemplatePositionTyp Type => this.Material != null ? TemplatePositionTyp.Material : TemplatePositionTyp.Service;
}
public enum TemplatePositionTyp
{
Material = 1,
Service = 2
}
Approach II:
public class Template
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<TemplateMaterialPosition> MaterialPositions { get; set; }
public ICollection<TemplateServicePosition> ServicePositions { get; set; }
}
public class TemplateMaterialPosition
{
public int Id { get; set; }
public int Count { get; set; }
public Material Material { get; set; }
public Template Template { get; set; }
}
public class TemplateServicePosition
{
public int Id { get; set; }
public int Count { get; set; }
public Service Service { get; set; }
public Template Template { get; set; }
}
I wish someone can explain me which one is the one to go and even the downside of the other approach.

Should I filter records in a view?

I'm a beginner at asp.net mvc, I'm building a web app where users can see who they owe money to, who owes money to them etc...
So now I'm curious where in my project should I filter the records for my database.
Lets say that I want to show how much money a user owes grouped by the names of users he owes it to.
This is my view model that I pass to the view:
public IEnumerable<Dug> Dugovanja { get; set; }
public Korisnik Korisnik { get; set; }
So the question is, should i filter the records from the database to show the grouped by result in the view directly, using methods in the view model (which I read was wrong) and simply calling them from the view, or should I add another property to the view model that would represent the filtered results and populate it in the controller?
The easiest way seems to be to filter them in the view directly, but then what if I have to use the same filtered results multiple times in the view?
Edit:
My debt class:
public class Dug
{
public int Id { get; set; }
[Column("Duznik")]
public int DuznikId { get; set; }
[Column("Vjerovnik")]
public int VjerovnikId { get; set; }
public decimal Iznos { get; set; }
public string Opis { get; set; }
public DateTime Datum { get; set; }
public decimal Uplata { get; set; }
public DateTime? Datum_Uplate { get; set; }
public bool Zatvoreno { get; set; }
public virtual Korisnik Duznik { get; set; }
public virtual Korisnik Vjerovnik { get; set; }
}
User class:
public class Korisnik
{
public int Id { get; set; }
public string Ime { get; set; }
public string Lozinka { get; set; }
}
User = Korisnik, Duznik = person who owes money, Vjerovnik = person to whom user owes money

Asp.Net MVC Reaching The Property Using Many To Many Relation With Mapping Table

You can see my previous question which related with many to many relation but with auto generated mapping table.
I have 2 model, HrTraining and HrPerson. Any people can be assigned to one or more Trainings. You can see my model as below
public class HrTraining
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<HrMapTrainingPerson> HrMapTrainingPerson { get; set; }
}
public class HrMapTrainingPerson
{
public int Id { get; set; }
public string Status { get; set; }
public int HrTrainingId { get; set; }
public int HrPersonId { get; set; }
public virtual HrTraining HrTraining { get; set; }
public virtual HrPerson HrPerson { get; set; }
}
public class HrPerson
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<HrMapTrainingPerson> HrMapTrainingPerson { get; set; }
}
How can i take all training objects which assingned to a person with efficient way.
So you want to find a person, and get all the trainings assigned to it? There are lot of ways.. but using your models, this could be something like this
var trPersons = dbContext.HrPerson.Find(idPerson).HrMapTrainingPerson.ToList();
foreach(var trPerson in trPersons) {
var training = trPerson.HrTraining;
//do what you want, here you can get trPerson.HrTraining.Name for instance
}

Newbie view model issue.. to subclass or not to subclass

Ok dead basic question, I'm a self taught developer so often I seem to have gaps where I can't decide which was is the right way... and this is one of them!! Simple I have a view model which has a collection of child items. But where these classes are defined I can't decide if the child object should be a subclass of the parent...
For example this:
public class ActionChartViewModel
{
public IEnumerable<ActionChartItemViewModel> Items { get; set; }
public TextPagingInfo TextPagingInfo { get; set; }
}
public class ActionChartItemViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Rating { get; set; }
public string Comment { get; set; }
public string AssignedToUserName { get; set; }
public string ContactRequested { get; set; }
public bool Resolved { get; set; }
public int NoteCount { get; set; }
public string ContactDetails { get; set; }
public int ResponseId { get; set; }
}
Or this:
public class ActionChartViewModel
{
public IEnumerable<Item> Items { get; set; }
public TextPagingInfo TextPagingInfo { get; set; }
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public string Rating { get; set; }
public string Comment { get; set; }
public string AssignedToUserName { get; set; }
public string ContactRequested { get; set; }
public bool Resolved { get; set; }
public int NoteCount { get; set; }
public string ContactDetails { get; set; }
public int ResponseId { get; set; }
}
}
I prefer the second one for a code readability and simplicity front, but I don't know the pros and cons of subclasses. What would you guys think??
Thanks in advance!!
I would use separate classes (in same file) as opposed to an inner class. Inner class would be useful when it serves only the parent class, i.e. would not be accessed from outside of the parent class, only by the parent class methods, etc. In your case the inner class needs to be used on view(s), so I don't see a need for it. The first option, i.e. separate classes, is actually simpler to me and reads better.
"SubClass" is when you create more concrete implementations (inherits) of its types. As # bloparod says, you're doing "inner classes". I also rarely use inner classes. Sometimes I use some private or internal classe as a temporary. If you do that, you will need to create with the sintaxe like:
ActionChartViewModel.Item item = new ActionChartViewModel.Item();
I usually separete files and use public classes but sometimes when I have lots and lots of ViewModel, I think a good pratice is to keep all of the same category of ViewModels on a single file and inherited when necessary, for sample:
File: ProductViewModel.cs
public class ProductViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string CategoryName { get; set; }
}
public class ProductDetailViewModel : ProductViewModel
{
public int Stocke { get; set; }
public string Obs { get; set; }
public IEnumerable<ProductMovViewModel> Inventory
/* other properties */
}
public class ProductMovViewModel
{
public int Id { get; set; }
public DateTime Date { get; set;
public int Amout { get; set; }
}
As a good pratice too you can separete in files your ViewModels, as you prefer.

Collection Nested within Model is Not Bound by MVC 3 (Always null)

I have a model that represents various information about a university in ASP.NET MVC 3 and Entity Framework 5.0. The model has an ICollection of another model, called TrendModel. This collection seems to never be stored/bound by MVC at any point, no matter what I do.
When I manually set this collection to something at run time (after it is retrieved from the database), the collection is of course no longer null, but whatever I seem to set it to and then store in the database, trends is always null when I retrieve it from the database.
UniversityModel:
public class UniversityModel
{
[Key]
public string univ_id { get; set; }
public string ipeds_id { get; set; }
public string name { get; set; }
public bool religious { get; set; }
#region Location Information
public string city { get; set; }
public string state { get; set; }
public string urbanization { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
#endregion
public ICollection<TrendModel> trends { get; set; }
}
TrendModel:
public class TrendModel
{
[Key]
public string id { get; set; }
public ushort year { get; set; }
public uint? capacity { get; set; }
public uint? rate { get; set; }
public uint? meals { get; set; }
public bool? forProfit { get; set; }
public bool? control { get; set; }
public string degree { get; set; }
public bool? landgrant { get; set; }
public bool? athletic { get; set; }
public string calendar { get; set; }
public bool? required { get; set; }
}
Not sure if it is relevant, but if I put in a constructor for UniversityModel that sets trends to an empty list, then trends is no longer null and is an empty list.
Is this a model binding issue, or a post issue or something? Sorry if I'm completely off-base, I'm pretty new to MVC and ASP.NET.
you haven't included a foreign key in your trend model.try adding univ_id in your TrendModel class.
public class TrendModel
{
[Key]
public string id { get; set; }
.
.
.
[ForeignKey("univ_id")]
public string univ_id {get;set;}
}
As it turn out, the issue was fixed simply by me enforcing lazy loading on the trends, so the property now reads:
public virtual ICollection<TrendModel> trends { get; set; }

Categories