1

DatabaseModel

I have the above Database model:

  • PD: Predefined (user has a list of options to choose from)
  • UA: User Added (user can create new entry's for this)
  • SA: System Added (generated in the controller when everything is saved)

I'm trying to think of a way of creating a 'over view' so i can create all the requirements for Report_Main on one page instead of having to create a view for Report_Main then directing the user to then another page for them to enter the Report_Peramiter_Fields etc.

I've had a look at View Models but i cant quite get my head around it, i get it for 'Index' and 'Detail' views pulling the data back. Its more for creating and editing the models.

Database: Sql Server
Data Mapping: Entity Framework
View Engine: razor
Liu
  • 970
  • 7
  • 19
Houlahan
  • 783
  • 3
  • 19
  • 46
  • What is your underlying data layer consisting of? Are you using Entity Framework? NHibernate? Please give some more details. – px06 Apr 06 '17 at 13:48
  • Apologies, i am currently using Entity Framework and Sql server – Houlahan Apr 06 '17 at 13:49

1 Answers1

1

ViewModels are a way to represent data on a View where you need to display information from Models, please have a look at this question to understand what they are: What is ViewModel in MVC?

In your case you can create a simple ViewModel to represent data that you need to display on Report_Main, this can be easily achieved by doing:

namespace MyProject 
{
    public class MyViewModel
    {
        public int ID { get; set; }
        public System_Tracking Tracking { get; set; }
        public Report_Login_Credentials Credentials { get; set; }
        public Report_Type Type { get; set; }
        public List<Report_Peramiter_Fields> Fields { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
    }
}

It may just look like a simple Model, because it is but it's only used by a view and it is not persisted. That is the difference here between a Model and a View Model.

From this you only need one page, say MyPage.cshtml where you can use this model like:

@model MyProject.MyViewModel

@{
    ViewBag.Title = "MyPage";
}

@* your content here *@

@Model.ID <br>
@Model.Report_Type.Description 

// Etc.

To pass this information you will need to do it in your controller, for example:

namespace MyProject
{
    public class MyController : Controller
    {
        public ActionResult MyPage(int? Id)
        {

            var data = context.Report_Main.Find(Id); // Or whatever

            var vm = new MyViewModel(){
                Tracking = data.System_Tracking,
                // ... populate the viewmodel here from the data received
            };

            return View(vm); 
        }
    }
}

In short, ViewModels allow you to bind all the data in one Model and pass it onto the view which can statically represent it, and you can also use this when getting data from a user when you don't want direct representation or access to a Model.

Edit: You can iterate through list of models in Razor by using C#:

foreach(var item in Model.Fields)
{
    <p>item.Peramiter</p>
}
Community
  • 1
  • 1
px06
  • 2,256
  • 1
  • 27
  • 47
  • Thanks for that is a massive incite in to view models! how would i go about with the Report_Peramiters side of things because that's a List of objects? – Houlahan Apr 06 '17 at 15:07
  • @Houlahan I've edited the answer to show how you can iterate results in Razor. However the way it looks to me is that `Report_Main_Export_Type` is the intermediate link table between `Report_Peramiter_Fields` and `Report_Export_Type`. In your Model you actually need something like: `List` but I can't say for sure because your tables look quite confusing without context. – px06 Apr 06 '17 at 15:37
  • i understand how to display the items in a list its adding new items to the list when creating or editing. Report_Export_Type is just a list of file formats excel, csv, pdf so a specific report can have the options of been exported to csv and then also pdf say – Houlahan Apr 07 '17 at 10:22