Class ViewModelBase
- Namespace
- Dynamicweb.Rendering
- Assembly
- Dynamicweb.dll
ViewModelBase represents a view model that can be used for rendering templates using a model instead of template tags.
Inherit from this base class to create a view model.
public abstract class ViewModelBase
- Inheritance
-
ViewModelBase
- Inherited Members
Examples
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering
{
/// <summary>
/// ViewModelSample shows how to implement a view model.
/// The class inherits from <see cref="ViewModelBase"/> so that it can be used in a view model template.
/// </summary>
public class ViewModelSample : ViewModelBase
{
public ViewModelSample()
{
}
public int Number { get; set; }
public string Text { get; set; }
}
}
using Dynamicweb.Rendering;
namespace Dynamicweb.Examples.Rendering
{
/// <summary>
/// ViewModelTemplateSample shows how to use a view model.
/// </summary>
public class ViewModelTemplateSample
{
/// <summary>
/// How to render a view model.
/// </summary>
public string RenderViewModel()
{
// create an instance of the view model
var model = new ViewModelSample();
model.Number = 123;
model.Text = "abc";
// create a template
var template = new Template("SampleTemplate.cshtml");
// set the view model
template.SetViewModel(model);
// render the template
return template.Output();
}
/// <summary>
/// How to render a view model or template tags depending on the selected template.
/// </summary>
public string RenderViewModelOrTags()
{
// create a template
var template = new Template("SampleTemplate.cshtml");
// check if the selected template is a view model template (@inherits ViewModelTemplate<>)
if (template.IsViewModelTemplate)
{
// create an instance of the view model
var model = new ViewModelSample();
model.Number = 123;
model.Text = "abc";
// set the view model
template.SetViewModel(model);
}
else
{
// set template tags
template.SetTag("Number", 123);
template.SetTag("Text", "abc");
}
// render the template
return template.Output();
}
}
}
Methods
ToJson()
public virtual string ToJson()