Scaffold MVC - Multiple Models per View - asp.net-mvc

I am trying to use scaffold my models.
My Model looks like this
public class Patient
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual int MRN { get; set; }
public virtual ICollection<Issue> Issues { get; set; }
}
public class Issue
{
public virtual int Id { get; set; }
public virtual int PatientId { get; set; }
public virtual Patient Patient { get; set; }
public virtual string Room { get; set; }
public virtual string Comment { get; set; }
}
So I would like it to be when the user adds a patient then the Create Scaffold would show the fields for the Patient and also for the Issue. What I am getting now is just the Patient fields. Is there a way to go about this?
I would like to give the option when creating a new patient, the customer gets the ability to add multiple Issues for the patient.

the default scaffolding stuff in MVC3, and even 4, to my knowledge will not handle this. there are too many different and complex variables for scaffolding to really be reliable in my opinion. you'll need to wire this up yourself. i would suggest creating an editor for your types. that will simplify things at least a little bit.

Related

ASP.NET MVC EF 6: define a lookup table/model

i am trying to define a database model in code-first to see and display which user is assigned as a specialist for the record data.
I have a very simple model for the user:
public class User
{
public int ID { get; set; }
public string userName { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
....
}
Next I have defined two (simple) models which define the data that can be edited by the user and the specialist should be assigned to using a dropdownlist:
public class Order
{
public int ID { get; set; }
public string orderNumber { get; set; }
public int specialistID { get; set; }
public virtual User specialist{ get; set; }
}
public class Part
{
public int ID { get; set; }
public string partNumber { get; set; }
public string description { get; set; }
public int specialistID { get; set; }
public virtual User specialist{ get; set; }
}
What kind of relation between the models can be used without having a navigation property for each table in the User model?
Do I need to use additional tables to define the relationship: User.Id-Order.specialistID and the relationship: User.Id-Part.specialistID ?
Is there a smarter way out-of-the-box by Entity Framework?
Many thanks for your answers.
Pascal
By default when you add forign-key constraint to the many-to-one table the Entity Framework add virtual property to the entity class and virtual ICollection to the User.

Entity Framework Many to Many Code First with DropDown List

I am new to MVC and was trying to get the Scaffolding to automagically create the drop down list in the PersonTitle for Person and Title. I got the drop down list to work however, the scaffolding function takes the first instance of a string as the value of the drop down. As in the case of the Person, the first name is used. However, that is not very informative.
I want to be able to modified it to contain the First and Last name. What is the best solution for this? I tried adding a FullName field but that has its own set of problems. Once of them being, I don't want the program/user to handle extra data entries.
Is there a better method of creating the drop down list for Person and Title after the scaffolding completes? What is the best way to add foreign key drop down list from other tables?
It would help if you provide code samples as I am new to MVC and the best practices solution as I would have to do this to several other tables.
Thanks much.
A person can have many titles
// Title -->>PersonTitle<<<----Person
public class PersonTitle
{
//person can have many titles
public int PersonTitleID { get; set; }
public int PersonID { get; set; }
public int TitleID { get; set; }
public virtual Person Person { get; set; }
public virtual Title Title { get; set; }
}
List of Titles
public class Title
{
public int TitleID { get; set; }
[Display(Name = "Title Name")]
public string TitleName { get; set; } //drop down text
public virtual ICollection<PersonTitle> PersonTitles { get; set; }
}
List of People
public class Person
{
public int PersonID { get; set; }
public string FirstName { get; set; } //drop down text is used
public string LastName { get; set; }
public virtual ICollection<PersonTitle> PersonTitles { get; set; }
}

ASP.NET MVC 4, EF5, Store additional data in many to many junction table

I have a Code First MVC 4 Web app - there's a many to many relationship between Places:
public virtual int PlaceID { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<User> Followers { get; set; }
and Users:
public virtual int UserID { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<Place> Places { get; set; }
Users can 'follow' many places, places can have many followers.
I would like to store additional information about the relationship - say a rating. So a user could have a rating for each place they follow, of between 1-10, say.
How can I achieve that - including CRUD operations (Code First, Entity Framework 5).
Thanks.
You will need to replace the many-to-many with two one-to-many relationships to an intermediate class that has the rating property which could be defined as below (you may need to add navigation attributes):
Create an intermediate class
public class UserPlaceRelationship {
public virtual int PlaceID { get; set; }
public virtual int UserID { get; set; }
public virtual Place Place { get; set; }
public virtual User User { get; set; }
public virtual int Rating { get; set; }
}
Update your places class:
public virtual int PlaceID { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<UserPlaceReationship> UserRelationships { get; set; }
and your users class:
public virtual int UserID { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<UserPlaceReationship> PlaceRelationships { get; set; }
Querying will be a little trickier as you will have to now navigate through the intermediate class.
You must create a junction table and add extra info to it.
Till now, there is no way to create a CRUD master-detail controller with related views.
You should follow the answer I provided to this question of yours...

Building view models and classes used in it

I'm searching for a good way for managing my view models and especially the classes used in these view models. I explain below with an example:
Let's say I would like to display a view which contains a project (title, content, category, ...) and below it a list of some related projects (of the same category). I created a view model especially for this view. Here it is:
public class ProjectDetailsViewModel
{
public ProjectFullViewModel OneProject { get; set; }
public IEnumerable<ProjectLightViewModel> RelatedProjects { get; set; }
// Below are the classes used in this view model
public class ProjectFullViewModel
{
public int ProjectID { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public string Category { get; set; }
public string Client { get; set; }
public int Year { get; set; }
public IEnumerable<Technology> Technologies { get; set; }
public byte[] ScreenshotData { get; set; }
public string ScreenshotName { get; set; }
public int ScreenshotLength { get; set; }
public string ScreenshotType { get; set; }
public byte[] BackgroundData { get; set; }
public string BackgroundName { get; set; }
public int BackgroundLength { get; set; }
public string BackgroundType { get; set; }
}
public class ProjectLightViewModel
{
public int ProjectID { get; set; }
public string Title { get; set; }
public string Slug { get; set; }
public string Category { get; set; }
public string Client { get; set; }
public string Year { get; set; }
}
}
As you can see, all classes used in this view model are contained in it. I think this is easier to keep en eye on what is used. What do you think? Is it a good/bad practice? Some suggestions? I noticed that when we have a lot of view models and classes used we can be a little confused. Don't blame me, I'm still learning ASP.NET MVC and I would like to make good choices...
Thanks.
I wouldn't use nested classes for view models. In order to reference them you will have to always specify the base class. That's could be particularly annoying when writing the mappings between your domain models and view models. If you are afraid of having many files containing your view models you could still place all the dependent view models inside the same .cs file as the parent model.
That is good practice. It is the use of View Models that allow you to pass or retrieve the necessary data to or from a View. That's what precisely makes them view models. Classes designed specifically for a View.
As for the concern of "too many" or confusion, that just comes down to your project/solution organization. Have enough logical separation so that it is evident where all your classes live.

Using VB.NET MVC3 and the Entity Framework "Code-First" method, how can I easily define multiple one-to-many relationships with the same model?

I'm very new to ASP.NET and could use some help.
For this scenario, I have 2 classes. One is a "project" class and the other is a "company" class. Essentially, what I need is one single "company directory" of all the companies we have relationships with, but I need to be able to freely slot them into 3 different slots within a project. It is possible that the same company could occupy all 3 slots, but it's equally likely that a different company will be placed in each slot.
Here are my classes:
public class Project
{
public int ID { get; set; }
public string Name { get; set; }
public int ClientID { get; set; }
public int PublisherID { get; set; }
public int DeveloperID { get; set; }
public Company Client { get; set; }
public Company Publisher { get; set; }
public Company Developer { get; set; }
}
public class Company
{
public int ID { get; set; }
public string Name { get; set; }
}
When I have used this basic outline in the past, the complex types I specify in the bottom half of the model definition will be auto generated based on the matching int ID properties specified earlier. For example, If I had a complex type "User" that was drawing it's data from a user table in my database, specifying (int UserID) within my class followed by (User User), the UserID field would be the actual field in my project table and the User object I specify will automatically be an object containing all the User information from the user table.
Using this method as I did in the classes specified above, however, does not work in the way I expected and instead creates not only ClientID, PublisherID, and DeveloperID but also creates CompanyID, CompanyID1, and CompanyID2 which are the fields that will actually be used when attempting to instantiate the Company objects I specified (even though those fields will contain null always).
Is there any way around this?
You just need to specify that your int properties are the foreign keys to your navigation properties.
public class Project
{
public int ID { get; set; }
public string Name { get; set; }
public int ClientID { get; set; }
public int PublisherID { get; set; }
public int DeveloperID { get; set; }
[ForeignKey("ClientID")]
public Company Client { get; set; }
[ForeignKey("PublisherID")]
public Company Publisher { get; set; }
[ForeignKey("DeveloperID")]
public Company Developer { get; set; }
}

Resources