Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

How to design View Models with EPI

Vote:
 

Hi,

I am developing a commernce site and need to deal with a lot of data. What's the best way to design the View Models and the Page types and block types? 

Curently, I am referring the Page Type inside the View Model and passing the model to the view. This is working fine. But when I need to use Blocks, this is not working. BlocksData is referred by the MainContentArea using @Html.PropertyFor and I am not finding any way to pass the data or view model to the Block template. I tried many options to pass the data and but nothing seems to work. 

Am I doing correct by referring the page type inside a View Model? A sample view model and controller is below.

public class DeliveryViewModel : BaseViewModel
{
///


/// Gets or sets the current page.
///

///
/// The current page.
///

public DeliveryPage CurrentPage { get; set; }

///


/// Gets or sets the Transaction data.
///

///
/// The conversion data.
///

public List TransactionData { get; set; }

///


/// Gets or sets the promotion code.
///

///
/// The promotion code.
///

public string PromotionCode { get; set; }

}

public class DeliveryPage : CommonOrderDetails
{
public virtual ContentArea MainContentArea { get; set; }
}

public class TransactionBlock : BlockData
{
[Display(
Name = "Header",
Description = "Enter a header for the block",
GroupName = SystemTabNames.Content,
Order = 1)]
[Required]
public virtual string Header { get; set; }

[Display(
Name = "Description",
Description = "Enter a Description for the block",
GroupName = SystemTabNames.Content,
Order = 2)]
[Required]
public virtual XhtmlString Description { get; set; }

}

public ActionResult Index(DeliveryPage currentPage)
{
var model = new DeliveryViewModel();
model.CurrentPage = currentPage;

return this.View(model);

}

public class TransactionBlockController : BlockController
{
public override ActionResult Index(TransactionBlock currentblock)
{
return PartialView(currentblock);
}

}

And Inside the Delivery-> Index.cshtml I am calling the MainContentArea like below

@foreach (var model in Model.TransactionData)
{@Html.PropertyFor(x => x.CurrentPage.MainContentArea)

}

Is this correct? Is there any better way?

Thanks

#149804
Jun 08, 2016 5:19
Vote:
 

Checkout the Alloy demo project way of handling viewmodel. I like that way.

Normally you just call Html.PropertyFor() to render the content area including any blocks within it. So that foreach loop looks wrong...

#150578
Jun 22, 2016 19:30
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.