Developer forum

Forum » Templates » Problem with accessing tags in razor template

Problem with accessing tags in razor template

Lasse Pedersen
Reply

Hello

I have a problem with a razor email template. 

I would like to set tags from the backend, because i want to avoid using httpcontext.

Template example:

<head>

</head>
@{

var customer = GetCustomer(GetValue("CustomerId"));

<body>

<strong>CustomerId : @GetValue("CustomerId")</strong>

</body>

}

When I use it in body, everything works fine, but i would like use it when i call GetCustomer so that I have a global variable, or even better, just pass an object to the template.

Is this even Possible? Or do i have to set a tag for each property in my customer object? 

 

/Lasse


Replies

 
Mikkel Ricky
Reply

You can pass an object to a Razor template using the Template.SetTagValue method (I'm using Dictionary, but it can be any object), e.g.

template.SetTagValue("data", new Dictionary<string, object> {
  { "Name", "Mikkel" }
});

and then use the passed object in your Razor template

@{
  var data = GetValue("data") as Dictionary<string, object>;
}
@data

You can also us the Dynamicweb API to get an object in your Razor template from some id available in the template, e.g.

@{
  var product = Dynamicweb.eCommerce.Products.Product.GetProductByID(GetString("Ecom:Product.ID"));
}
@product.Name

I hope this is what you're looking for.

Best regards,
Mikkel

 

 

You must be logged in to post in the forum