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.
c# sql asp.net-mvc entity-framework
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.
add a comment |
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.
c# sql asp.net-mvc entity-framework
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.
add a comment |
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.
c# sql asp.net-mvc entity-framework
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
c# sql asp.net-mvc entity-framework
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.
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.
add a comment |
add a comment |
active
oldest
votes
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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