Dynamic and Typed Views
You can pass a Model class containing data to the views as normal:
public ActionResult Typed() { return View("eve-Typed.LandingPage", Models.LandingPageModel.GetSample()); }
When using the resource name as view name, the view engine will not process directly the information passed in the Model. The reason for this is that in EV5 you have to explicitly opt in for any processing. Opting in happens in the view class, by its definition.
When using the simplest definition of EmbeddedView
public class SimpleHtmlView : EmbeddedViewthe passed object will be transformed to dynamic type, essentially it will bypass static type checking.
You can however use the generic version of the class EmbeddedView, to enforce static type checking on the passed Model:
public class LandingPageView : EmbeddedView<LandingPageModel>
Accessing Model from View class
You can access the Model data in the view after initialization via the Model property.
public override void ProcessView(ViewContext viewContext) { var node = this.HtmlDocument .Document .SelectSingleNode("//h1"); if (node != null) node.InnerHtml = Model.Header.Header1; }
DataBinding from HTML
You can also access the Model's values from the HTML via a the eve-eval attribute
<div class="col-lg-8 align-self-baseline"> <p class="text-white-75 mb-5" eve-eval="Header2 ">Start Bootstrap can help you build better websites using the Bootstrap framework! Just download a theme and start customizing, no strings attached!</p> <a class="btn btn-primary btn-xl" href="#about">Find Out More</a> </div>
In order to use this feature you have to explictly opt in for processing these attributes in the view class. One of the ways to do it is to override the BeforeProcessView method, that is called just before the ProcessView method.
protected override void BeforeProcessView(ViewContext viewContext) { base.BeforeProcessView(viewContext); this.HtmlDocument.Document .ProcessEvals(this.Model); }
Models of Partial Views
Models can be complex and you can match the Model to the structure of the views if needed. Thus you can have a page model with a header footer and multiple content parts. For example:
public class LandingPageModel { public Header Header { get; set; } public IList<Content> Contents { get; set; } internal static LandingPageModel GetSample() { return new LandingPageModel() { Header = new Header() { Header1 = "::Header 1 from Model - via view processing", Header2 = "::Header 2 from Model - via databinding" }, Contents = new List<Content> { new Content(){ Heading = "::Content 1 Heading from Model", Lead = "::Content 1 Lead from Model" }, new Content(){ Heading = "::Content 2 Heading from Model", Lead = "::Content 2 Lead from Model" }, new Content(){ Heading = "::Content 3 Heading from Model", Lead = "::Content 3 Lead from Model" }, }, }; } }When defining the partial view, you can also define the part of the model that applies to the partial view via the eve-partialmodel attribute.
<!-- Masthead--> <div eve-partial="eve-Typed.HeaderPartial" eve-renderinstead eve-partialmodel="Header"> </div> <!-- Content--> <div eve-partial="eve-EV5.VE-Assets.Sample.Typed.Content.html" eve-renderinstead eve-partialmodel="Contents"> </div>Click here to see the above in action. Observe the code to see the full example.