Copied to clipboard

Flag this post as spam?

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


  • Jacob Brock-Hansen 13 posts 155 karma points
    Mar 14, 2022 @ 10:06
    Jacob Brock-Hansen
    0

    Submit forms with complex models

    Hi - im trying something that might not be the best approach so feel free to correct me :-)

    I basically have a website built in Bootstrap with a form i want submitted and validated with ModelState.IsValid and displayed with the standard Bootstrap display.

    So i have this view:

    @using Umbraco.Cms.Core.Models.PublishedContent;
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.Claim>
    @{
        Layout = "Master.cshtml";
        PolicyAndContactInfo pacModel = Model.Descendant<PolicyAndContactInfo>();
    }
    <div>
       @using (Html.BeginUmbracoForm<ClaimFormController>("CreateClaimRequest", null, new { @class = "full-panel-form", @role = "form" }))
                {
                    <div class="console-form-body">
                        <div class="row">           
                            <div class="col-lg-12 col-md-12">
                                <div class="form-group  has-feedback">
                                    <label class="flex">Email</label>
                                    <div class="input-group">
                                        <div class="input-group-prepend"><span class="input-group-text"><i class="fa fa-key"></i></span></div>
                                        @Html.TextBoxFor(m => pacModel.EmailAddress, new { @class = "form-control" })
                                    </div>
                                    @Html.ValidationMessageFor(m => pacModel.EmailAddress, null, new { @class = "text-danger" })
                                </div>
                            </div>
                        </div>
                    </div>
                }
    </div>
    

    Its just an email address i want to submit to a controller that will validate it with:

    [HttpPost]
    public async Task<IActionResult> CreateClaimRequest(ClaimRequest request)
    {
        if (ModelState.IsValid)
        {
            //Do something here, send an email or something
        }
        return CurrentUmbracoPage();
    }
    

    The ClaimRequest class would just be a simple class i create so i can add [Required] annotations to it and that would make the ModelState.IsValid to set failed modelState when the email is not filled out.

    This usually works just fine and let me do nice validations, but im getting an error with this setup because i tried do better organize my umbraco content nodes, so i have one main content node with settings for the website and a descendant node to hold my parameters - im using it in the view like this:

    PolicyAndContactInfo pacModel = Model.Descendant<PolicyAndContactInfo>();
    

    but then i create the input parameter in the form with:

    @Html.TextBoxFor(m => pacModel.EmailAddress, new { @class = "form-control" })
    

    The resulting input name is "pacModel.EmailAddress" and i cant add a parameter with a dot in my request class.

    Am i just doing this the wrong way?

  • iNETZO 134 posts 497 karma points c-trib
    Mar 15, 2022 @ 21:20
    iNETZO
    100

    Hi Jacob,

    Does it work when you add a bind prefix to the controller action? Like:

    public async Task<IActionResult> CreateClaimRequest([Bind(Prefix = "pacModel")] ClaimRequest request)
    

    If your model contains arrays or other complex stuf, the default TypeFormatter could be an problem.

    Best regards,

    iNETZO

  • Jacob Brock-Hansen 13 posts 155 karma points
    Mar 23, 2022 @ 06:19
    Jacob Brock-Hansen
    0

    Hi That worked, thank you for providing the solution

Please Sign in or register to post replies

Write your reply to:

Draft