Copied to clipboard

Flag this post as spam?

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


  • Chirdeep Tomar 8 posts 28 karma points
    Feb 17, 2018 @ 10:50
    Chirdeep Tomar
    0

    Cannot bind source type Umbraco.Web.Models.RenderModel to model type ViewModel.

    Hi All,

    I have a simple contact form which was working until I upgraded to umbraco 7.8.1.

    Now I am getting the following error: Cannot bind source type Umbraco.Web.Models.RenderModel to model type DeltaSports.Core.ViewModels.ContactFormViewModel.

    <!-- Form -->
    @Html.Partial("~/Views/Partials/Site/Contact/ContactForm.cshtml", new ContactFormViewModel())
    

    ContactFormController.cs

    public class ContactFormController : SurfaceController
        {
            [HttpPost]
            public async Task<ActionResult> Submit(ContactFormViewModel model)
            {
                if (!ModelState.IsValid)
                    return CurrentUmbracoPage();
    
                await Mailer.Mail(model);
                return RedirectToCurrentUmbracoPage();
            }
        }
    

    ContactFormViewModel.cs

    public class ContactFormViewModel
        {
            [Required(ErrorMessage = "Name is required")]
            public string Name { get; set; }
    
            [Required(ErrorMessage = "Phone is required")]
            [EmailAddress(ErrorMessage = "Phone is required")]
            public string Phone { get; set; }
    
            [Required(ErrorMessage = "Email is required")]
            [EmailAddress(ErrorMessage = "Email is required")]
            public string Email { get; set; }
    
            [Required(ErrorMessage = "Message is required")]
            public string Message { get; set; }
        }
    

    Please let me know what could make this working code break in 7.8.1.

    Thanks Chirdeep

    -------------UPDATED--------------------

    Partial View

    @model DeltaSports.Core.ViewModels.ContactFormViewModel
    
    
    <div class="col-md-6 p-b-30">
    
        @using (Html.BeginUmbracoForm("Submit", "ContactForm", FormMethod.Post, new { name = "contact-form", id = "contact-form", css = "leave-comment" }))
        {
            <h4 class="m-text26 p-b-36 p-t-15">
                Send us your message
            </h4>
    
            <div class="bo4 of-hidden size15 m-b-20">
                @Html.TextBoxFor(m => m.Name, new { @class = "sizefull s-text7 p-l-22 p-r-22", placeholder = Html.NameFor(n => n.Name) })
                @Html.ValidationMessageFor(x => Model.Name, "Please enter a name", new { @class = "text-danger" })
            </div>
    
            <div class="bo4 of-hidden size15 m-b-20">
                @Html.TextBoxFor(m => m.Phone, new { @class = "sizefull s-text7 p-l-22 p-r-22", placeholder = Html.NameFor(n => n.Phone) })
                @Html.ValidationMessageFor(x => Model.Phone, "Please enter a phone number", new { @class = "text-danger" })
            </div>
    
            <div class="bo4 of-hidden size15 m-b-20">
                @Html.TextBoxFor(m => m.Email, new { @class = "sizefull s-text7 p-l-22 p-r-22", placeholder = Html.NameFor(n => n.Email) })
                @Html.ValidationMessageFor(x => Model.Email, "Please enter an email address", new { @class = "text-danger" })
            </div>
    
            @Html.TextAreaFor(m => m.Message, new { @class = "sizefull s-text7 p-l-22 p-r-22", cols = "30", rows = "7", placeholder = Html.NameFor(n => n.Message) })
            @Html.ValidationMessageFor(x => Model.Message, "Please enter a message", new { @class = "text-danger" })
    
            <div class="w-size25">
                <!-- Button -->
                <button class="flex-c-m size2 bg1 bo-rad-23 hov1 m-text3 trans-0-4">
                    Send
                </button>
            </div>
        }
    </div>
    
  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Feb 17, 2018 @ 12:06
    Marc Goodson
    0

    Hi Chirdeen

    Can you post the details of your partial view? a

    In particular keen to see what how you have wired up the form...eg

    @using (Html.BeginUmbracoForm <ContactFormController>("Submit")) 
    

    When you post to a surface controller, you need to use BeginUmbracoForm which then includes a hidden field that handles the routing to your surface controller action.

    (There is an equivalent helper called Url.SurfaceAction, if you just want a specific Url to post to)

    The error message you are seeing, I've usually seen before in the context of a custom RenderMvcController - have you any custom Route Hijacking RenderMvcControllers in your solution, eg a doc type called ContactForm that could be interfering.

    I've cut and pasted what you have above into a vanilla 7.8 solution and I'm finding it's working as expected :-(

    (although I'm guessing how you are posting to the surface controller with the contact form, which is why I think that might be the bit that is going awry)

    One other unrelated point, I know you've had this working before upgrade, but if you check your model validation rule for Phone - you'll see you are insisting people enter a valid Email address for their telephone number!!!!!

      [Required(ErrorMessage = "Phone is required")]
            [EmailAddress(ErrorMessage = "Phone is required")]
            public string Phone { get; set; }
    

    So if that form is live somewhere, you may not be getting many people filling it in!

    Sorry to not be able to exactly pinpoint the issue!

    regards

    Marc

  • Chirdeep Tomar 8 posts 28 karma points
    Feb 17, 2018 @ 15:31
    Chirdeep Tomar
    0

    Thanks Marc. I have updated the question. Also tried to use BeginForm like you suggested and removed the wrong phone validation but still no luck.

  • Daniel Chenery 119 posts 465 karma points
    Feb 18, 2018 @ 10:12
    Daniel Chenery
    1

    What's the URL of the page, and is there a document type of the same name? E.G. mysite.com/contact and a document type of Contact

    Like Marc says, it sounds like an issue you would run into with custom routing.

    I'm wondering if alternative templates is causing a problem (although I would expect that to say a document type model name, and not render model)

  • Chirdeep Tomar 8 posts 28 karma points
    Feb 19, 2018 @ 18:07
    Chirdeep Tomar
    0

    Hi Dan,

    URL: http://localhost:61491/contact

    and yes there is a document type called contact.

  • Daniel Chenery 119 posts 465 karma points
    Feb 19, 2018 @ 23:29
    Daniel Chenery
    0

    Hi Chirdeep,

    Try changing the URL of the page. See if that will get it working (it doesn't have to be a permanent change)

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Feb 18, 2018 @ 10:48
    Marc Goodson
    0

    Hi Chirdeep

    I have cut and paste your UmbracoBeginForm into my partial, and can confirm it all works as expected, so the hunch is there is something in your solution that is conflicting with the routing of the post.

    First thing I'd try is renaming your controller, to ContactFormTestController, and wiring up your form to use that, it would rule out something clashing, doc type alias with 'ContactForm' and go from there.

    regards

    Marc

  • Chirdeep Tomar 8 posts 28 karma points
    Feb 19, 2018 @ 18:08
    Chirdeep Tomar
    0

    Made the changes you suggested and still no luck.

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Feb 18, 2018 @ 17:26
    Nik
    1

    Hi Chirdeep,

    Something that stands out to me is your use of this line in the partial view:

    @model DeltaSports.Core.ViewModels.ContactFormViewModel
    

    I don't believe you should be using @model in views for Umbraco. To add your custom model as the view model replace this line with:

    @inherits UmbracoViewPage<DeltaSports.Core.ViewModels.ContactFormViewModel>
    

    Nik

  • Chirdeep Tomar 8 posts 28 karma points
    Feb 19, 2018 @ 18:08
    Chirdeep Tomar
    0

    Hi Nik,

    Made the change and still no luck.

  • Chirdeep Tomar 8 posts 28 karma points
    Feb 20, 2018 @ 14:07
    Chirdeep Tomar
    0

    I had Contact.cshtml also inheriting from UmracoViewPage

    Thanks all for your help.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Feb 21, 2018 @ 23:15
    Marc Goodson
    0

    Cool, glad you got it sorted!

Please Sign in or register to post replies

Write your reply to:

Draft