Copied to clipboard

Flag this post as spam?

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


  • nickornotto 403 posts 907 karma points
    Aug 08, 2017 @ 15:02
    nickornotto
    0

    How to return validation errors from RegisterMember partial macro?

    I have a page with a partial macro RegisterModel and simple action to register new user. Basically based on this articles:

    https://24days.in/umbraco-cms/2015/extending-membership/ https://24days.in/umbraco-cms/2014/dealing-with-members/

    I want to return validation errors from ModelState to the page/ macro if user exists or any other validation or status error is returned.

    How do I do it?

    var model = new SupplierRegisterModel();
    
        using (Html.BeginUmbracoForm<MembersController>("MemberRegister"))
        {
            <fieldset>
                <legend></legend>
    
                    @Html.ValidationSummary("model", true)
                    <div class="form-group">
                        @Html.LabelFor(m => model.CompanyName)
                        @Html.TextBoxFor(m => model.CompanyName)
                        @Html.ValidationMessageFor(m => model.CompanyName)
                    </div>
                    <div class="form-group">
                        @Html.LabelFor(m => model.FirstName)
                        @Html.TextBoxFor(m => model.FirstName)
                        @Html.ValidationMessageFor(m => model.FirstName)
                    </div>
                <div class="form-group">
                    @Html.LabelFor(m => model.Surname)
                    @Html.TextBoxFor(m => model.Surname)
                    @Html.ValidationMessageFor(m => model.Surname)
                </div>
                    <div class="form-group">
                        @Html.LabelFor(m => model.Email)
                        @Html.TextBoxFor(m => model.Email, new { @class = "text ui-widget-content ui-corner-all" })
                        @Html.ValidationMessageFor(m => model.Email)
                    </div>
                    <div class="form-group">
                        @Html.LabelFor(m => model.Password)
                        @Html.PasswordFor(m => model.Password)
                        @Html.ValidationMessageFor(m => model.Password)
                    </div>
    
                    @Html.HiddenFor(m => model.MemberTypeAlias)
                    @Html.HiddenFor(m => model.RedirectUrl)
                    @Html.HiddenFor(m => model.UsernameIsEmail)
    
                    <div class="form-group">
                        <button type="submit" class="btn btn-default">Register as supplier</button>
                        @if (TempData["Status"] != null)
                        {
                            <p>
                                @TempData["Status"]
                            </p>
                        }
                    </div>
    
            </fieldset>
        }
    

    This:

    return View()
    

    cannot be done as the macro is embedded in a page.

    This:

    return PartialView()
    

    will render just the partial content without the layout.

    In addition partial cannot be strongly typed so I have no means to return the model into the partial to display ModelState errors.

    With:

    return RedirectToCurrentUmbracoUrl()
    

    I can just reload the page with TempData.

    I have no idea how to return errors into the ValidationSummary.

    Any ideas how this things are handled with macro partials in umbraco 7?

  • Marc Goodson 2157 posts 14435 karma points MVP 10x c-trib
    Aug 09, 2017 @ 08:40
    Marc Goodson
    100

    Hi Manila

    I would move your form into a partial view, strongly typed to your SupplierRegisterModel eg

    @inherits UmbracoViewPage<SupplierRegisterModel>
    

    and then in your Macro Partial, I would use Html.RenderPartial to display this form passing in a new instance of the model

    @{ Html.RenderPartial("~/Views/Partials/registersupplier.cshtml", new SupplierRegisterModel()); }
    

    Now in your surface controller when there is a modelstate error you can return CurrentUmbracoPage as this keeps the ModelState intact and displays the validation errors! (whereas RedirectToCurrentUmbracoPage wipes your modelstate and validation errors.

    eg

    if (!ModelState.IsValid) { return CurrentUmbracoPage(); } else { TempData["success"] = true; return RedirectToCurrentUmbracoPage(); }

    if that makes sense?

    regards

    marc

  • nickornotto 403 posts 907 karma points
    Aug 10, 2017 @ 11:08
    nickornotto
    0

    Marc, thanks. It works great!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies