1
votes

I have a simple Widget that I've created using a ContentPart. The ContentPart has the appropriate Driver, the Widget can be placed anywhere in my Orchard site and everything works great! The Widget needs to capture some data from the user, validate it, process it and then display a success response in the Widget.

To achieve this I've defined a controller that accepts a POST request that will process the Model and return the same MyForm view if it is invalid or return the FormSuccess view if it's valid. The Widget Part template uses @Html.Partial("MyForm") to display the MyForm view that contains an Ajax form that will update it's containing div with the POST response. The controller will return the MyForm view or the FormSuccess view depending on the data.

The problem that I'm having is that my designer needs to override the partial views for the MyForm view and FormSuccess view, he can easily override the Part display template but not the partial views.

I think the reason that this isn't possible at the moment is because I'm using @Html.Partial("MyForm"). I've found that if I use @Display(New.MyForm()) instead, I can override the MyForm view by creating a file called MyForm.cshtml in the Views folder for the current theme, this is exactly what I need, but how do I do the equivalent for @Display in the controller action?

At the moment I'm doing return this.PartialView("MyForm", model) or return this.PartialView("FormSuccess") which is going to use the original views and not the overridden ones. I've noticed that there is a ShapeResult but I'm not sure if this is the right thing to do and I don't know how to generate the dynamic constructor parameter. Also ideally I need the MyForm view to have a strongly typed model so that I can use the HtmlHelper methods LabelFor, TextboxFor and ValidationMessageFor which isn't possible when the model is a dynamic?

Has anybody done something like this before or can anyone offer any guidance?

Thanks, Jason

2

2 Answers

0
votes

Have you tried something like this?

public ActionResult YourAction(){
  //where yourContent is a your widget content
  //and _contentManager is IContentManager
  dynamic model = _contentManager.BuildDisplay(yourContent);
  return View((object)model);
}
0
votes

I managed to get this working by changing the way my module was being compiled! I don't completely understand how this has worked, but it has!

Initially I tried overriding the partial views by creating files with the same name in the views folder for the current theme, this did nothing and I've been trying different approaches ever since.

What I've found is that if I force my module to be dynamically compiled the partial views from the themes folder override the related views in my module. Previously I was using the referenced module loader because I wanted to debug my module and this was the only way I could achieve it.

It appears that when a module is dynamically compiled something happens that maps the views from my module to the overriding views in the theme!

Here's a good link that explains module loading.