I have a simple ascx contact us control that sends an email.
I have been using it over time to various umbraco websites running versions from 4 up to 6.1.5 and it has always worked fine. I have been also tried it with various versions of .NET without problems.
This time I updated it to .NET 4.5 and tested it and then added it in an Umbraco 7 project. It renders fine but when the submit button is pressed the page reloads without a postback and nothing happens. There is no error message in the logs, there is no activity from the form.
The code is such that if there is an error that would be handled and shown on the screen after a postback but the form simply reloads.
I have tried all the usual checks and I can't find absolutely nothing wrong with it. Can anyone suggest why the control wouldn't work on Umbraco 7 or how to find the error that causes a full page refresh?
I had actually suspected that but I have already written all my templates using razor so I was hoping there would be some backward compatibility with plugging an ascx control in.
I will rewrite the control as an MVC partial view...
Is there an example anywhere regarding that? Would help me speed things up a lot
My MVC control cannot render within the RichEditor because the RichEditor chooses a default model for it:
Exception: System.InvalidOperationException: The model item passed into the dictionary is of type 'Umbraco.Web.Models.PartialViewMacroModel', but this dictionary requires a model item of type 'Models.ContactModel'.
Also I tried switiching the umbracoSetting to Forms again:
Basically I followed the page you sent but I believe it was written before Umbraco 7 and doesn't reference the Micropartials section which is where the control needs to go in order to be accessible by Umbraco 7 so I think that guide is a bit out of date.
I am wondering if I could get away with somehow posting the form using ajax but I would need an example to follow to do this as it woud take too many hours of experimentation otherwise.
The best example I found is this one which is more up to date than the link you provided and also I had too many problems with the implementation of the first link.
I created a view Model:
public class ContactFormViewModel
{
[Required]
public string Name { get; set; }
[Required]
[RegularExpression(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")]
public string Email { get; set; }
public string Telephone { get; set; }
[Required]
public string Message { get; set; }
public string ToAddress { get; set; }
}
Then a surface controller:
public class ContactUsController : SurfaceController
{
public ActionResult RenderContactForm()
{
return PartialView("Contact", new ContactFormViewModel());
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult HandleContactSubmit(ContactFormViewModel model)
{
if (ModelState.IsValid == false)
{
return CurrentUmbracoPage();
}
var msg = new MailMessage(model.Email, model.ToAddress);
msg.IsBodyHtml = true;
var sb = new StringBuilder();
sb.Append("bla blah" + model.Message);
msg.Subject = "blah";
msg.Body = sb.ToString();
try
{
var client = new SmtpClient();
client.Send(msg);
TempData.Add("Success", true);
}
catch (Exception ex)
{
TempData.Add("Error", true);
}
return RedirectToCurrentUmbracoPage();
}
}
And finally a Partial View called Contact.cshtml which I dumped into the Micropartials folder of Umbraco because this is the only way to make it visible in the macro menu. The view looks like this:
@using MyProject.Controllers
@using Umbraco.Web
@model MyProject.Models.ContactFormViewModel
@{
Html.EnableClientValidation(true);
Html.EnableUnobtrusiveJavaScript(true);
var success = false;
var error = false;
if (TempData["Success"] != null)
{
success = bool.Parse(TempData["Success"].ToString());
}
if (TempData["Error"] != null)
{
error = bool.Parse(TempData["Error"].ToString());
}
}
@if (success)
{
Thanks, we'll get back to you soon!
}
else if (error)
{
Error!
}
else
{
using (Html.BeginUmbracoForm("HandleContactSubmit", new {@id="sendEmailForm"}))
{
Then I dump the MyProject.dll into the bin folder and create the macro exactly as I did with with the forms version and I try to add the macro into the RichText editor and I immediately get the error:
custom ascx control not working with Umbraco 7
I have a simple ascx contact us control that sends an email.
I have been using it over time to various umbraco websites running versions from 4 up to 6.1.5 and it has always worked fine. I have been also tried it with various versions of .NET without problems.
This time I updated it to .NET 4.5 and tested it and then added it in an Umbraco 7 project. It renders fine but when the submit button is pressed the page reloads without a postback and nothing happens.
There is no error message in the logs, there is no activity from the form.
The code is such that if there is an error that would be handled and shown on the screen after a postback but the form simply reloads.
I have tried all the usual checks and I can't find absolutely nothing wrong with it. Can anyone suggest why the control wouldn't work on Umbraco 7 or how to find the error that causes a full page refresh?
Thank you
Hi Nick
That's probably because that Umbraco 7 is using the MVC engine out of the box rather than the Webforms engine.
Since version 4.10 it's been possible to use MVC in Umbraco but the default setting has Webforms as default untill know.
So if you want to keep using webforms you need to change the template engine in the /config/umbracoSettings.config from "Mvc" to "Webforms".
Hope this helps.
/Jan
I had actually suspected that but I have already written all my templates using razor so I was hoping there would be some backward compatibility with plugging an ascx control in.
I will rewrite the control as an MVC partial view...
Is there an example anywhere regarding that? Would help me speed things up a lot
Hi Nick
Well as such there is backwards compatibilty but just not when you need to do a postback since there is no
My MVC control cannot render within the RichEditor because the RichEditor chooses a default model for it:
Exception: System.InvalidOperationException: The model item passed into the dictionary is of type 'Umbraco.Web.Models.PartialViewMacroModel', but this dictionary requires a model item of type 'Models.ContactModel'.
Also I tried switiching the umbracoSetting to Forms again:
<defaultRenderingEngine>WebForms</defaultRenderingEngine>
In a desperate move to get the control working and the behaviour is still the same.
At this point all I want is a simple form on my site and I am unable to do it with Umbraco 7. Any ideas how to add this simple form?
Hi Nick
Could you share what your MVC code for the form looks like? What exact version of Umbraco 7 are you using?
/Jan
Hi Jan,
The version is
7.0.4 assembly: 1.0.5161.21054
And the code I can post it first thing tomorrow.
Basically I followed the page you sent but I believe it was written before Umbraco 7 and doesn't reference the Micropartials section which is where the control needs to go in order to be accessible by Umbraco 7 so I think that guide is a bit out of date.
I am wondering if I could get away with somehow posting the form using ajax but I would need an example to follow to do this as it woud take too many hours of experimentation otherwise.
The best example I found is this one which is more up to date than the link you provided and also I had too many problems with the implementation of the first link.
I created a view Model:
Then a surface controller:
And finally a Partial View called Contact.cshtml which I dumped into the Micropartials folder of Umbraco because this is the only way to make it visible in the macro menu. The view looks like this:
Then I dump the MyProject.dll into the bin folder and create the macro exactly as I did with with the forms version and I try to add the macro into the RichText editor and I immediately get the error:
The formatting is terrible.
Much better here
Okay it works if I write this on the template and the form behaves correctly:
However I need to add it via the Rich Editor because it should appear in a specific point in the content, the same way ascx macros used to be added.
Is that something unsupported by Umbraco 7?
The plumbing for it appears to still be there but it doesn't work
is working on a reply...