I have a strongly typed viewpage that I created that looks like this:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Models.Reports.OverallResultsViewModel>" %>
I'm trying to create the view page in code and render it without displaying it :
System.Web.Mvc.ViewPage<Models.Reports.OverallResultsViewModel> page2 = new ViewPage<Models.Reports.OverallResultsViewModel>();
Here is the implemented view model:
public class OverallResultsViewModel {
public OverallResultsViewModel(string name, string draft, IEnumerable<Subject> subjects, IEnumerable<IResult> results) {
this.Name = name;
this.Draft = draft;
this.Subjects = subjects;
this.Results = results;
}
public IEnumerable<Subject> Subjects { get; set; }
public IEnumerable<IResult> Results { get; set; }
public string Name { get; set; }
public string Draft { get; set; }
}
I was thinking I'd use the following code to pass in the values to the view model:
page2.Model.Draft = Draft;
page2.Model.Results = page2Data;
page2.Model.Subjects = subjects;
page2.Model.Name = Name;
But I get an error saying that Model is null. How do I get Model initialized so that I can set variables on it?
Try:
page2.ViewData.Model = new OverallResultsViewModel(
Name,
Draft,
subjects,
page2Data
);