Building view models and classes used in it - asp.net-mvc

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.

Related

Viewmodel set up Aspt.net MVC 6

I'm having trouble understanding how to implement a ViewModel in Asp.net MVC, I have the following tables:
Form
ID, Data
Report
ID, FormID, Owner, Category, Status, SubmissionDate
ReportValues
ID, ReportID, Title, Value
I'm looking for a way to display and edit Report and ReportValues in the one ViewModel where ReportValues.ReportID = Report.ID
ReportValues will have multiple entries that relate to a Report.
I have had a look at similiar questions on here and tried following a tutorial ( http://techfunda.com/howto/262/list-data-using-viewmodel ) and coming up empty handed.
If you need any more information let me know and thanks in advance for any replies!
Your View Model is nothing more than a class. You can solve this many ways, but here's an example.
Create your 3 classes like you normally would.
public class Form
{
public int Id { get; set; }
public string Data { get; set; }
}
public class ReportValues
{
public int Id { get; set; }
public int ReportId { get; set; }
public string Title { get; set; }
public string Value { get; set; }
}
public class Report
{
public int Id { get; set; }
public int FormId { get; set; }
public string Owner { get; set; }
public string Category { get; set; }
public string Status { get; set; }
public DateTime SubmissionDate { get; set; }
}
Then, create your ViewModel class to include the three above classes like this.
public class ReportViewModel
{
public Form Form { get; set; }
public ReportValues ReportValues { get; set; }
public Report Report { get; set; }
}
In your view you can access your three classes and their properties as you would in your controller. Model.Form.Id
Depending on your data types, ReportValues will likely be a property of Report, but that's entirely up to your data structure. You will need to populate the classes using whatever method you want (Entity Framework, ADO, etc.) before you can pass them to your view and use them.

MVC 5 Model. Set Property to Database or Entity Framework value

The model Survey CRUD's the Questions:
public class Survey
{
public int SurveyID { get; set; }
public string Question1 { get; set; }
public string Question2 { get; set; }
public string Question3 { get; set; }
public string Question4 { get; set; }
public string Question5 { get; set; }
}
This model IntroResponse should have Properties that should be set to the values set in Survey. (IntroResponse is read only, not Creating or Editing Question properties):
public class IntroResponse : IValidatableObject
{
public int IntroResponseID { get; set; }
public int SurveyID { get; set; }
[ForeignKey("SurveyID")]
public virtual Survey Survey { get; set; }
public string Question1 { get; set; }
public string Question2 { get; set; }
public string Question3 { get; set; }
public string Question4 { get; set; }
public string Question5 { get; set; }
public IntroResponse()
{
//Code here?
}
As you can see these two models have a realtionship that is:
SurveyID 1 -----> * IntroResponse
But I want to inherit more properties from the Survey model. Is this possible without adding Controller code at all?
Shall I retrieve this information via Entity Framework or simply get values from Database?
I could not find any earlier threads about this specific question. I really looked.
Please give me an idea about what is the best practices to achieve this.
Update:
What I want to have is the Questions automatically retrieved and displayed when I select SurveyID with the dropdown. This would require client-side code, right? So should I have JavaScript code make a database call to get the Questions? I don't want to mix JavaScript and C#/Razor code in this way. That is why I am asking for some pointer!

using entity framework code first in mvc with view model?

so when implementing entity framework code first in mvc, do we separate the view restrictions from view model? this is because for database first the model is generated(so i see the reason to separate it to view model but how about code first?)
The next questions i would ask is it ok to separate view model to another folder? since by default asp.net is MVC there is no view model inside
Model <--- what is this model call? data model? domain model? business model?
public class Student
{
public int ID { get; set; }
[StringLength(250)]
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
}
View Model
public class Student
{
public int ID { get; set; }
[MaxLength(250)]
[Required]
public string LastName { get; set; }
[Required]
public string FirstMidName { get; set; }
[Required]
public DateTime EnrollmentDate { get; set; }
}
Your model that Used in mvc views is viewmodel.
your model that persist in database is domain model.
Your domain model may has some properties that you don't need use it in your client.
Your Service layer must return Dto (data transfer object) to your client and you can map dto to viewmodel .
First Question:
You should use partial class and metadata to seperate , just like below:
[MetadataType(typeof(StudentMD))]
public partial class Student
{
public class StudentMD
{
public int ID { get; set; }
[MaxLength(250)]
[Required]
public string LastName { get; set; }
[Required]
public string FirstMidName { get; set; }
[Required]
public DateTime EnrollmentDate { get; set; }
}
}
Second Question:
It's OK to add a folder name "View Model"
I did it in my project too!

Entity Framework defining tables in db context

I am buys designing the model below:
public class LogModel
{
public class UserActivityLogs
{
[Key]
public int id { get; set; }
//Id of the user
public string userId { get; set; }
//Time of the log
public DateTime time { get; set; }
public LogActions action { get; set; }
}
// Types of actions to log
public class LogActions
{
[Key]
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
}
}
Now what I would like to know is do I need to add a table in the context for Logactions as well as UserActivityLogs or will EF see that the two tables are linked and create the log action table automatically?
Also have I specified my relationships correctly? What I was aiming for is that I can define multiple types of Logactions and then a userlog will then have a single log action associated to it.
First, don't use nested classes, it's a needless complication. Use namespaces to organize classes.
Second, don't use plural names for classes. One instance of class represents one entity. Also, use CamelCase names for properties.
Third, yes, Entity Framework will be aware of the associations between the two classes and create a database model with two tables and a foreign key.
So this leaves you with:
namespace MyApp.LogModel
{
public class UserActivityLog
{
[Key]
public int Id { get; set; }
public string UserId { get; set; }
public DateTime Time { get; set; }
public LogAction LogAction { get; set; }
}
public class LogAction
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}

Scaffold MVC - Multiple Models per View

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.

Resources