MVC EF Using table view to populate a ViewModel











up vote
0
down vote

favorite












I find this kind of hard to formulate as I just started C# a few months ago and I'm now creating a website in ASP.NET using MVC model and razor pages and Entity Framework, while also using Stored Procedures and Views in my database. All completely new elements and with relatively low amount of help online for the specefics having to piece different answers together. So far that has worked fine but I'm currently in need of creating an IEnumerable version of an existing ViewModel based on a sql View.



The reason is quite simple and another solution might exist I've got the following ViewModel:



public class TimeRegistrationViewModel
{
public int PK_Id { get; set; }
[Required]
public int FK_UserId { get; set; }
[Required]
public int FK_ProjectId { get; set; }
[Required]
public int FK_TaskId { get; set; }
[Required]
public int Time { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public System.DateTime Date { get; set; }
[Required]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

public System.DateTime DateEntry { get; set; }
public string TaskTypeName { get; set; }
public string ProjectName { get; set; }
public string UserName { get; set; }
public List<TimeReg.VI_TimeRegistration> VI_TimeRegistration_List { get; set; }
public TimeRegistrationViewModel() { }
//Overload created for shorter controller code when assigned the values of View to a ViewModel
public TimeRegistrationViewModel(VI_TimeRegistration viTimeRegistration)
{
PK_Id = viTimeRegistration.PK_Id;

FK_UserId = viTimeRegistration.FK_UserId;
FK_ProjectId = viTimeRegistration.FK_ProjectId;
FK_TaskId = viTimeRegistration.FK_TaskId;
Time = viTimeRegistration.Time;
Date = viTimeRegistration.Date;
DateEntry = viTimeRegistration.DateEntry;
TaskTypeName = viTimeRegistration.TaskTypeName;
ProjectName = viTimeRegistration.ProjectName;
UserName = viTimeRegistration.UserName;
}

public TimeRegistrationViewModel(List<TimeReg.VI_TimeRegistration> list)
{
VI_TimeRegistration_List = list;
}
}


}



As you can see it uses the data validitation method of:



DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]


In order to display only the DATE on the index page instead of DATE: 00:00:00



Now this works under the details page however the way I've made my controller for index is like so:



   public ActionResult Index()
{
var timeRegistrationViewModel = new TimeRegistrationViewModel(db.VI_TimeRegistration.ToList());
return View(timeRegistrationViewModel);
}


So this ofcourse doesn't use the datavalidation on the index page.



What I kind of want to do is in essence:



    public ActionResult Index()
{
IEnumerable<TimeRegistrationViewModel> timeRegistrationViewModel = new TimeRegistrationViewModel(db.VI_TimeRegistration);
return View(timeRegistrationViewModel);
}


Which obviously doesn't work due to the different classes but I'm not quite sure how to achieve the result I want. I've looked into AutoMapper but I don't quite understand it.



I've looked into a foreach loop but with my minimal experience with IEnumeralbe I'm not sure how to approach this either as I can't create an abstract IList or IEnumerable of my TimeRegistrationViewModel:



And as mentioned I'm not sure if I can apply the data validation elsewhere. Easiest place would be on the VI_TimeRegistration entity but that's inside Entity Frame and therefore get overridden.










share|improve this question







New contributor




MiniLysgaard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote

    favorite












    I find this kind of hard to formulate as I just started C# a few months ago and I'm now creating a website in ASP.NET using MVC model and razor pages and Entity Framework, while also using Stored Procedures and Views in my database. All completely new elements and with relatively low amount of help online for the specefics having to piece different answers together. So far that has worked fine but I'm currently in need of creating an IEnumerable version of an existing ViewModel based on a sql View.



    The reason is quite simple and another solution might exist I've got the following ViewModel:



    public class TimeRegistrationViewModel
    {
    public int PK_Id { get; set; }
    [Required]
    public int FK_UserId { get; set; }
    [Required]
    public int FK_ProjectId { get; set; }
    [Required]
    public int FK_TaskId { get; set; }
    [Required]
    public int Time { get; set; }
    [Required]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public System.DateTime Date { get; set; }
    [Required]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

    public System.DateTime DateEntry { get; set; }
    public string TaskTypeName { get; set; }
    public string ProjectName { get; set; }
    public string UserName { get; set; }
    public List<TimeReg.VI_TimeRegistration> VI_TimeRegistration_List { get; set; }
    public TimeRegistrationViewModel() { }
    //Overload created for shorter controller code when assigned the values of View to a ViewModel
    public TimeRegistrationViewModel(VI_TimeRegistration viTimeRegistration)
    {
    PK_Id = viTimeRegistration.PK_Id;

    FK_UserId = viTimeRegistration.FK_UserId;
    FK_ProjectId = viTimeRegistration.FK_ProjectId;
    FK_TaskId = viTimeRegistration.FK_TaskId;
    Time = viTimeRegistration.Time;
    Date = viTimeRegistration.Date;
    DateEntry = viTimeRegistration.DateEntry;
    TaskTypeName = viTimeRegistration.TaskTypeName;
    ProjectName = viTimeRegistration.ProjectName;
    UserName = viTimeRegistration.UserName;
    }

    public TimeRegistrationViewModel(List<TimeReg.VI_TimeRegistration> list)
    {
    VI_TimeRegistration_List = list;
    }
    }


    }



    As you can see it uses the data validitation method of:



    DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]


    In order to display only the DATE on the index page instead of DATE: 00:00:00



    Now this works under the details page however the way I've made my controller for index is like so:



       public ActionResult Index()
    {
    var timeRegistrationViewModel = new TimeRegistrationViewModel(db.VI_TimeRegistration.ToList());
    return View(timeRegistrationViewModel);
    }


    So this ofcourse doesn't use the datavalidation on the index page.



    What I kind of want to do is in essence:



        public ActionResult Index()
    {
    IEnumerable<TimeRegistrationViewModel> timeRegistrationViewModel = new TimeRegistrationViewModel(db.VI_TimeRegistration);
    return View(timeRegistrationViewModel);
    }


    Which obviously doesn't work due to the different classes but I'm not quite sure how to achieve the result I want. I've looked into AutoMapper but I don't quite understand it.



    I've looked into a foreach loop but with my minimal experience with IEnumeralbe I'm not sure how to approach this either as I can't create an abstract IList or IEnumerable of my TimeRegistrationViewModel:



    And as mentioned I'm not sure if I can apply the data validation elsewhere. Easiest place would be on the VI_TimeRegistration entity but that's inside Entity Frame and therefore get overridden.










    share|improve this question







    New contributor




    MiniLysgaard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I find this kind of hard to formulate as I just started C# a few months ago and I'm now creating a website in ASP.NET using MVC model and razor pages and Entity Framework, while also using Stored Procedures and Views in my database. All completely new elements and with relatively low amount of help online for the specefics having to piece different answers together. So far that has worked fine but I'm currently in need of creating an IEnumerable version of an existing ViewModel based on a sql View.



      The reason is quite simple and another solution might exist I've got the following ViewModel:



      public class TimeRegistrationViewModel
      {
      public int PK_Id { get; set; }
      [Required]
      public int FK_UserId { get; set; }
      [Required]
      public int FK_ProjectId { get; set; }
      [Required]
      public int FK_TaskId { get; set; }
      [Required]
      public int Time { get; set; }
      [Required]
      [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
      public System.DateTime Date { get; set; }
      [Required]
      [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

      public System.DateTime DateEntry { get; set; }
      public string TaskTypeName { get; set; }
      public string ProjectName { get; set; }
      public string UserName { get; set; }
      public List<TimeReg.VI_TimeRegistration> VI_TimeRegistration_List { get; set; }
      public TimeRegistrationViewModel() { }
      //Overload created for shorter controller code when assigned the values of View to a ViewModel
      public TimeRegistrationViewModel(VI_TimeRegistration viTimeRegistration)
      {
      PK_Id = viTimeRegistration.PK_Id;

      FK_UserId = viTimeRegistration.FK_UserId;
      FK_ProjectId = viTimeRegistration.FK_ProjectId;
      FK_TaskId = viTimeRegistration.FK_TaskId;
      Time = viTimeRegistration.Time;
      Date = viTimeRegistration.Date;
      DateEntry = viTimeRegistration.DateEntry;
      TaskTypeName = viTimeRegistration.TaskTypeName;
      ProjectName = viTimeRegistration.ProjectName;
      UserName = viTimeRegistration.UserName;
      }

      public TimeRegistrationViewModel(List<TimeReg.VI_TimeRegistration> list)
      {
      VI_TimeRegistration_List = list;
      }
      }


      }



      As you can see it uses the data validitation method of:



      DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]


      In order to display only the DATE on the index page instead of DATE: 00:00:00



      Now this works under the details page however the way I've made my controller for index is like so:



         public ActionResult Index()
      {
      var timeRegistrationViewModel = new TimeRegistrationViewModel(db.VI_TimeRegistration.ToList());
      return View(timeRegistrationViewModel);
      }


      So this ofcourse doesn't use the datavalidation on the index page.



      What I kind of want to do is in essence:



          public ActionResult Index()
      {
      IEnumerable<TimeRegistrationViewModel> timeRegistrationViewModel = new TimeRegistrationViewModel(db.VI_TimeRegistration);
      return View(timeRegistrationViewModel);
      }


      Which obviously doesn't work due to the different classes but I'm not quite sure how to achieve the result I want. I've looked into AutoMapper but I don't quite understand it.



      I've looked into a foreach loop but with my minimal experience with IEnumeralbe I'm not sure how to approach this either as I can't create an abstract IList or IEnumerable of my TimeRegistrationViewModel:



      And as mentioned I'm not sure if I can apply the data validation elsewhere. Easiest place would be on the VI_TimeRegistration entity but that's inside Entity Frame and therefore get overridden.










      share|improve this question







      New contributor




      MiniLysgaard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I find this kind of hard to formulate as I just started C# a few months ago and I'm now creating a website in ASP.NET using MVC model and razor pages and Entity Framework, while also using Stored Procedures and Views in my database. All completely new elements and with relatively low amount of help online for the specefics having to piece different answers together. So far that has worked fine but I'm currently in need of creating an IEnumerable version of an existing ViewModel based on a sql View.



      The reason is quite simple and another solution might exist I've got the following ViewModel:



      public class TimeRegistrationViewModel
      {
      public int PK_Id { get; set; }
      [Required]
      public int FK_UserId { get; set; }
      [Required]
      public int FK_ProjectId { get; set; }
      [Required]
      public int FK_TaskId { get; set; }
      [Required]
      public int Time { get; set; }
      [Required]
      [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
      public System.DateTime Date { get; set; }
      [Required]
      [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

      public System.DateTime DateEntry { get; set; }
      public string TaskTypeName { get; set; }
      public string ProjectName { get; set; }
      public string UserName { get; set; }
      public List<TimeReg.VI_TimeRegistration> VI_TimeRegistration_List { get; set; }
      public TimeRegistrationViewModel() { }
      //Overload created for shorter controller code when assigned the values of View to a ViewModel
      public TimeRegistrationViewModel(VI_TimeRegistration viTimeRegistration)
      {
      PK_Id = viTimeRegistration.PK_Id;

      FK_UserId = viTimeRegistration.FK_UserId;
      FK_ProjectId = viTimeRegistration.FK_ProjectId;
      FK_TaskId = viTimeRegistration.FK_TaskId;
      Time = viTimeRegistration.Time;
      Date = viTimeRegistration.Date;
      DateEntry = viTimeRegistration.DateEntry;
      TaskTypeName = viTimeRegistration.TaskTypeName;
      ProjectName = viTimeRegistration.ProjectName;
      UserName = viTimeRegistration.UserName;
      }

      public TimeRegistrationViewModel(List<TimeReg.VI_TimeRegistration> list)
      {
      VI_TimeRegistration_List = list;
      }
      }


      }



      As you can see it uses the data validitation method of:



      DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]


      In order to display only the DATE on the index page instead of DATE: 00:00:00



      Now this works under the details page however the way I've made my controller for index is like so:



         public ActionResult Index()
      {
      var timeRegistrationViewModel = new TimeRegistrationViewModel(db.VI_TimeRegistration.ToList());
      return View(timeRegistrationViewModel);
      }


      So this ofcourse doesn't use the datavalidation on the index page.



      What I kind of want to do is in essence:



          public ActionResult Index()
      {
      IEnumerable<TimeRegistrationViewModel> timeRegistrationViewModel = new TimeRegistrationViewModel(db.VI_TimeRegistration);
      return View(timeRegistrationViewModel);
      }


      Which obviously doesn't work due to the different classes but I'm not quite sure how to achieve the result I want. I've looked into AutoMapper but I don't quite understand it.



      I've looked into a foreach loop but with my minimal experience with IEnumeralbe I'm not sure how to approach this either as I can't create an abstract IList or IEnumerable of my TimeRegistrationViewModel:



      And as mentioned I'm not sure if I can apply the data validation elsewhere. Easiest place would be on the VI_TimeRegistration entity but that's inside Entity Frame and therefore get overridden.







      c# sql asp.net-mvc entity-framework






      share|improve this question







      New contributor




      MiniLysgaard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      MiniLysgaard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      MiniLysgaard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 19 at 12:04









      MiniLysgaard

      11




      11




      New contributor




      MiniLysgaard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      MiniLysgaard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      MiniLysgaard is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          MiniLysgaard is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374268%2fmvc-ef-using-table-view-to-populate-a-viewmodel%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          MiniLysgaard is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          MiniLysgaard is a new contributor. Be nice, and check out our Code of Conduct.













          MiniLysgaard is a new contributor. Be nice, and check out our Code of Conduct.












          MiniLysgaard is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374268%2fmvc-ef-using-table-view-to-populate-a-viewmodel%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory