Is it good practice to populate the models/variables of a viewmodel in the constructor of the viewmodel?
For instance:
public class ProgramViewModel
{
public IEnumerable<Programme> ProgramList { get; set; }
public string QuerystringAgeID { get; set; }
public ProgramViewModel()
{
QuerystringAgeID = HttpContext.Current.Request.QueryString["QuerystringAgeID"];
}
}
It depends.
But with the example you have shown, the answer is no. You have a model binder that is supposed to do that:
and then:
In addition to that you should absolutely avoid using
HttpContext.Currentin an ASP.NET MVC application. Makes your code tied to an ASP.NET context making it impossible to reuse and unit test in isolation. ASP.NET MVC provides you abstractions for this: HttpContextBase, …