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>
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!
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.
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.
ContactFormController.cs
ContactFormViewModel.cs
Please let me know what could make this working code break in 7.8.1.
Thanks Chirdeep
-------------UPDATED--------------------
Partial View
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
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!!!!!
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
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.
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 ofContact
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)
Hi Dan,
URL: http://localhost:61491/contact
and yes there is a document type called contact.
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)
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
Made the changes you suggested and still no luck.
Hi Chirdeep,
Something that stands out to me is your use of this line in the partial view:
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:
Nik
Hi Nik,
Made the change and still no luck.
I had Contact.cshtml also inheriting from UmracoViewPage
Thanks all for your help.
Cool, glad you got it sorted!
is working on a reply...