Copied to clipboard

Flag this post as spam?

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


  • Jason 92 posts 175 karma points
    Oct 15, 2015 @ 21:02
    Jason
    0

    Creating custom contact form, on Bazaar store site

    Hello, I'm trying to create contact form on a bazaar store. I've done very similar without issue on other Umbraco sites. I get a 'No parameterless constructor' error.

    I've been unable to identify what's causing this, despite stepping through with visual studio.

    Here's the format:

    New document type: 'ContactForm'

    Hijacked the route:

      public class ContactFormController : RenderControllerBase
    {
    
        public override ActionResult Index(RenderModel model)
        {
            var viewModel = new ContactUsModel(model.Content); { };
            viewModel.CurrentCustomer = this.CurrentCustomer;
            return View(viewModel.ThemeViewPath("ContactForm"), viewModel);
        }
    }
    

    Model (inherits from MasterModel for the ICustomerBase)

       public class ContactUsModel : MasterModel
    {
        public ContactUsModel(IPublishedContent content) : base(content)
        {
        }
    
        [Required]
        public string Name { get; set; }
        [Required]
        public string TelephoneNumber { get; set; }
        [EmailAddress]
        public string Email { get; set; }
        [Required]
        public string Message { get; set; }
    
        public int ThankYouPage { get; set; }
    }
    

    ContactForm view:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Models.Contact.ContactUsModel>
    @{
    Layout = "Master.cshtml";
    }
    
    @Html.ThemedPartial(Model.Theme, "_ContactUs", new ContactUsModel(Model){ThankYouPage = Model.GetPropertyValue<int>("thankyoupage")})
    

    Partial view:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<Models.Contact.ContactUsModel>
    
    @using (var form = Html.BeginUmbracoForm<ManxIndependentTradingGroup.Controllers.Contact.ContactFormSurfaceController>("HandleFormSubmit"))
    {
    @Html.AntiForgeryToken()
    
    <div class="row">
        <div class="col-xs-12 col-md-6 col-md-offset-3">
            <div class="">
                <label class="form-label" for="">Contact Name @Html.ValidationMessageFor(m => m.Name)</label>
                <div class="input-group">
                    @Html.TextBoxFor(m => m.Name, new { @class = "form-field", placeholder = "Enter Name", required = "required" })
                </div>
            </div>
            <div class="">
                <label class="form-label" for="">Telephone Number @Html.ValidationMessageFor(m => m.TelephoneNumber)</label>
                <div class="input-group">
                    @Html.TextBoxFor(m => m.TelephoneNumber, new { @class = "form-field", placeholder = "Enter Telephone Number", required = "required" })
                </div>
            </div>
            <div class="form-group">
                <label class="form-label" for="">Email Address @Html.ValidationMessageFor(m => m.Email)</label>
                <div class="input-group">
                    @Html.TextBoxFor(m => m.Email, new { @class = "form-field", placeholder = "Enter Email", required = "required" })
                </div>
            </div>
            <div class="form-group">
                <label class="form-label" for="">Message @Html.ValidationMessageFor(m => m.Message)</label>
                <div class="input-group">
                    @Html.TextAreaFor(m => m.Message, new { @class = "form-field", rows = 8, required = "required" })
                </div>
            </div>
    
            @Html.HiddenFor(x => x.ThankYouPage)
    
            <input type="submit" name="submit" id="submit" value="Submit">
        </div>
    </div>
    }
    

    At this point the page loads as expected, with the contact form. On submit of the form I receive the 'No parameterless constructor'

    Surface controller - my breakpoints aren't being hit on this class..

     public class ContactFormSurfaceController : SurfaceController
    {
        [ValidateAntiForgeryToken()]
        public ActionResult HandleFormSubmit(ContactUsModel model)
        {
            if (!ModelState.IsValid)
            {
                return CurrentUmbracoPage();
            }
    
            //send email
    
            return RedirectToUmbracoPage(model.ThankYouPage);
    
        }
    }
    

    Would appreciate some assistance here guys.

    cheers

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Oct 15, 2015 @ 21:20
    Rusty Swayne
    1

    @Jason,

    I think the problem is you are posting a model that inherits from PublishedContentModel.

    It would probably be more straight forward to publish a simple POCO model (one that has a default constructor with no parameters).

    In your contact page, you could either add a ContactFormModel and pass it to partial which had the form or use an Html.Action to render the form ...

  • Jason 92 posts 175 karma points
    Oct 15, 2015 @ 21:31
    Jason
    0

    @Rusty

    Many thanks. I went ahead and created a POCO model as suggested - working fine now.

    cheers

Please Sign in or register to post replies

Write your reply to:

Draft