I am trying to make a MVC form as a macro. Even though it sees right in the back-end on the front-end it doesn't render itself correcly. Can someone help me?
My guess here is that a MacroPartial is a special kind of Partial View in Umbraco, it can receive Parameter values entered by editors when the Macro is inserted, and to achieve this it must @inherits a specific Model:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
But this then mucks up adding an MVC form like you are attempting here as you want the Model for this partial view to match your Form Fields eg
@model Umbraco.Model.ContactViewModel
But in a partial view you can only have one or the other!
The workaround therefore is to create a new 'normal' Partial View in your Views/Partials folder, called something like RenderContactForm.cshtml
Move your MVC form implementation into this Partial View with it's reference to your ContactViewModel at the top.
If you only need to render your form in a template then you do not need to use a Macro at all, you can just write:
@Html.Partial("/views/partials/RenderContactForm.cshtml", new ContactViewModel())
But if you do need a Macro, eg so editors can choose to add the Form in the grid or Rich Text Area, then create a separate Macro Partial and make sure it has
@inherits Umbraco.Web.Macros.PartialViewMacroPage
at the top...
and then all that Macro Partial needs to do is render the normal MVC partial view eg:
@Html.Partial("/views/partials/RenderContactForm.cshtml", new ContactViewModel())
I have done exactly as you said but still the front end looks as before, it's seems like Html.BeginUmbracoForm doesn't generate a form tag, instead it generates a "umb-macro-holder".
How can I make it work, should I import a library or something?
I just had to try this out to confirm what might be going on!!
I'm using 8.7RC
I Create a Model:
namespace Umbraco_8_7_RC.Models
{
public class ContactViewModel
{
public string Name { get; set; }
public string Email { get; set; }
public string Message { get; set; }
}
}
and a Controller:
namespace Umbraco_8_7_RC.Controllers
{
public class ContactController : SurfaceController
{
// GET: Contact
public ActionResult Submit(ContactViewModel vm)
{
return View();
}
}
}
When I use either a grid or a rich text area, and choose to insert the contact form macro, and publish, then ...
I get the form fields in the backoffice AND on the frontend...
So you can see from my example, it's really similar to what you have!
The only difference perhaps that I'm using @Inherits instead of @model at the top of the partial view? and that my models arent' coming from a namespace with the name 'Umbraco' - could that be conflicting?
In the end I found out that it doesn't work only when I insert the macro in the RichTextEditor. If I insert the macro outside of the RichTextEditor then the macro renders on the front-end.
Shouldn't it work both ways? I mean both in RTE and alone.
Macro and MVC forms in Umbraco 8
Hello there,
I am trying to make a MVC form as a macro. Even though it sees right in the back-end on the front-end it doesn't render itself correcly. Can someone help me?
Here is how it looks on the back-end:
and here is how it looks on the front-end:
And below you can see the MacroPartialView:
Does anybody know what can be the issue here?
Hi Valentin
My guess here is that a MacroPartial is a special kind of Partial View in Umbraco, it can receive Parameter values entered by editors when the Macro is inserted, and to achieve this it must @inherits a specific Model:
But this then mucks up adding an MVC form like you are attempting here as you want the Model for this partial view to match your Form Fields eg
But in a partial view you can only have one or the other!
The workaround therefore is to create a new 'normal' Partial View in your Views/Partials folder, called something like RenderContactForm.cshtml
Move your MVC form implementation into this Partial View with it's reference to your ContactViewModel at the top.
If you only need to render your form in a template then you do not need to use a Macro at all, you can just write:
But if you do need a Macro, eg so editors can choose to add the Form in the grid or Rich Text Area, then create a separate Macro Partial and make sure it has
at the top...
and then all that Macro Partial needs to do is render the normal MVC partial view eg:
regards
marc
Hi Marc, thank you for your reply.
I have done exactly as you said but still the front end looks as before, it's seems like Html.BeginUmbracoForm doesn't generate a form tag, instead it generates a "umb-macro-holder".
How can I make it work, should I import a library or something?
Hi Valentin
Which option did you go with?
Calling the Html Partial directly from the template
or calling the Html Partial from within the Macro Partial?
regards
Marc
Hi Marc, I chose to go with the second option, calling the HTML Partial from within the Macro Partial.
Hi Valentin
That outter div with class umb-macro-holder is generated by the Macro Partial...
is the issue not having an @ before the using statement, and so the template 'doesn't know' to write out the form in the template eg:
make any difference for you?
regards
Marc
Hi Marc, it still doesn't work, this is how my MacroPartial looks like:
And this is how Contact.cshtml looks like (the file that is referenced from the MacroPartial . You can see that now I added the @.
On the front-end Umbraco still doesn't render a form while in the back-end the form looks as expected with all the text boxes vissible.
Moreover, the text is underlined for some reason.
Hi Valentin
I just had to try this out to confirm what might be going on!!
I'm using 8.7RC
I Create a Model:
and a Controller:
and a partial view:
and then a Macro Partial:
When I use either a grid or a rich text area, and choose to insert the contact form macro, and publish, then ...
I get the form fields in the backoffice AND on the frontend...
So you can see from my example, it's really similar to what you have!
The only difference perhaps that I'm using @Inherits instead of @model at the top of the partial view? and that my models arent' coming from a namespace with the name 'Umbraco' - could that be conflicting?
But sure is strange!
Hi again Marc,
In the end I found out that it doesn't work only when I insert the macro in the RichTextEditor. If I insert the macro outside of the RichTextEditor then the macro renders on the front-end.
Shouldn't it work both ways? I mean both in RTE and alone.
Hi Valentin
In my example above, I'm inserting it in a Rich Text Editor, and I also tried in the Grid, and it works!
I'm not sure what the difference is, I'm using V8.7RC, which version are you running?
Does your versions now match what I have above?
(It is strange and yes should work in both!)
regards
Marc
Hi again Marc
Right now I am running Umbraco 8.6.4. Maybe I should try to upgrade it to V8.7?
Now I have upgraded my website to V8.7, but I still have the same problem.
is working on a reply...