Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Valentin Valeanu 29 posts 152 karma points
    Aug 28, 2020 @ 08:36
    Valentin Valeanu
    0

    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:

    back-end and here is how it looks on the front-end:

    front-end

    And below you can see the MacroPartialView:

    @model Umbraco.Model.ContactViewModel
    using (Html.BeginUmbracoForm<Umbraco.Controller.ContactController>("Submit"))
        {
            <div>
                <p>
                    @Html.LabelFor(m => m.Name)
                    @Html.TextBoxFor(m => m.Name)
                    @Html.ValidationMessageFor(m => m.Name)
                </p>
            </div>
            <div>
                <p>
                    @Html.LabelFor(m => m.Email)
                    @Html.TextBoxFor(m => m.Email)
                    @Html.ValidationMessageFor(m => m.Email)
                </p>
            </div>
            <div>
                <p>
                    @Html.LabelFor(m => m.Message)
                    @Html.TextAreaFor(m => m.Message)
                    @Html.ValidationMessageFor(m => m.Message)
                </p>
            </div>
    
            <input type="submit" value="Send" /> <input type="reset" value="Reset" />
            <p><div class="g-recaptcha" data-sitekey="6LexiiETAAAAAMcC8Fjj72f3MPJmgi3Kxmil8D8v"></div></p>
        }
    

    Does anybody know what can be the issue here?

  • Marc Goodson 1247 posts 8423 karma points MVP 5x c-trib
    27 days ago
    Marc Goodson
    1

    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:

    @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())
    

    regards

    marc

  • Valentin Valeanu 29 posts 152 karma points
    26 days ago
    Valentin Valeanu
    0

    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?

    front-end

  • Marc Goodson 1247 posts 8423 karma points MVP 5x c-trib
    26 days ago
    Marc Goodson
    1

    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

  • Valentin Valeanu 29 posts 152 karma points
    23 days ago
    Valentin Valeanu
    0

    Hi Marc, I chose to go with the second option, calling the HTML Partial from within the Macro Partial.

  • Marc Goodson 1247 posts 8423 karma points MVP 5x c-trib
    23 days ago
    Marc Goodson
    1

    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:

    @using (Html.BeginUmbracoForm<Umbraco.Controller.ContactController>("Submit")){
    ...
    
    }
    

    make any difference for you?

    regards

    Marc

  • Valentin Valeanu 29 posts 152 karma points
    23 days ago
    Valentin Valeanu
    0

    Hi Marc, it still doesn't work, this is how my MacroPartial looks like:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @Html.Partial("~/Views/Partials/Contact.cshtml", new Umbraco.Models.ContactViewModel())
    

    And this is how Contact.cshtml looks like (the file that is referenced from the MacroPartial . You can see that now I added the @.

    @model Umbraco.Models.ContactViewModel
    
    @using (Html.BeginUmbracoForm<Umbraco.Controllers.ContactController>("Submit"))
    {
        <div>
            <p>
                @Html.LabelFor(m => m.Name)
                @Html.TextBoxFor(m => m.Name)
                @Html.ValidationMessageFor(m => m.Name)
            </p>
        </div>
        <div>
            <p>
                @Html.LabelFor(m => m.Email)
                @Html.TextBoxFor(m => m.Email)
                @Html.ValidationMessageFor(m => m.Email)
            </p>
        </div>
        <div>
            <p>
                @Html.LabelFor(m => m.Message)
                @Html.TextAreaFor(m => m.Message)
                @Html.ValidationMessageFor(m => m.Message)
            </p>
        </div>
    
        <input type="submit" value="Send" /> <input type="reset" value="Reset" />
        <p>
            <div class="g-recaptcha" data-sitekey="6LexiiETAAAAAMcC8Fjj72f3MPJmgi3Kxmil8D8v"></div>
        </p>
    }
    

    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.

  • Marc Goodson 1247 posts 8423 karma points MVP 5x c-trib
    22 days ago
    Marc Goodson
    1

    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:

    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();
            }
        }
    }
    

    and a partial view:

    @inherits UmbracoViewPage<Umbraco_8_7_RC.Models.ContactViewModel>
    
    @using (Html.BeginUmbracoForm<Umbraco_8_7_RC.Controllers.ContactController>("Submit"))
    {
        <div>
            <p>
                @Html.LabelFor(m => m.Name)
                @Html.TextBoxFor(m => m.Name)
                @Html.ValidationMessageFor(m => m.Name)
            </p>
        </div>
        <div>
            <p>
                @Html.LabelFor(m => m.Email)
                @Html.TextBoxFor(m => m.Email)
                @Html.ValidationMessageFor(m => m.Email)
            </p>
        </div>
        <div>
            <p>
                @Html.LabelFor(m => m.Message)
                @Html.TextAreaFor(m => m.Message)
                @Html.ValidationMessageFor(m => m.Message)
            </p>
        </div>
    
        <input type="submit" value="Send" /> <input type="reset" value="Reset" />
        <p>
            <div class="g-recaptcha" data-sitekey="6LexiiETAAAAAMcC8Fjj72f3MPJmgi3Kxmil8D8v"></div>
        </p>
    }
    

    and then a Macro Partial:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
       @Html.Partial("~/Views/Partials/Contact.cshtml",new Umbraco_8_7_RC.Models.ContactViewModel())
    

    When I use either a grid or a rich text area, and choose to insert the contact form macro, and publish, then ...

    enter image description here

    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!

  • Valentin Valeanu 29 posts 152 karma points
    20 days ago
    Valentin Valeanu
    0

    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.

  • Marc Goodson 1247 posts 8423 karma points MVP 5x c-trib
    20 days ago
    Marc Goodson
    0

    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

  • Valentin Valeanu 29 posts 152 karma points
    16 days ago
    Valentin Valeanu
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft