Copied to clipboard

Flag this post as spam?

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


  • Nick 101 posts 123 karma points
    May 21, 2014 @ 16:21
    Nick
    0

    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

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 21, 2014 @ 18:24
    Jan Skovgaard
    0

    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

  • Nick 101 posts 123 karma points
    May 21, 2014 @ 18:32
    Nick
    0

    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


     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 21, 2014 @ 18:35
    Jan Skovgaard
    0

    Hi Nick

    Well as such there is backwards compatibilty but just not when you need to do a postback since there is no

    stuff.

    But if you just add some user controls that reads data it should work fine.

    But I guess you should then consider converting your user control to a surface controller instead. You should be able to read about this here http://umbraco.com/follow-us/blog-archive/2013/7/14/moving-from-webforms-to-mvc.aspx

    Hope this helps.

    /Jan

  • Nick 101 posts 123 karma points
    May 21, 2014 @ 20:07
    Nick
    0

    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? 

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 21, 2014 @ 20:44
    Jan Skovgaard
    0

    Hi Nick

    Could you share what your MVC code for the form looks like? What exact version of Umbraco 7 are you using?

    /Jan

  • Nick 101 posts 123 karma points
    May 21, 2014 @ 21:21
    Nick
    0

    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.

     

     

  • Nick 101 posts 123 karma points
    May 21, 2014 @ 21:26
    Nick
    0

    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.

  • Nick 101 posts 123 karma points
    May 22, 2014 @ 13:43
    Nick
    0
  • Nick 101 posts 123 karma points
    May 22, 2014 @ 13:45
    Nick
    0

    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"}))
        {
    @Html.LabelFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) @Html.EditorFor(model => model.Name)
    @Html.LabelFor(model => model.Email) @Html.ValidationMessageFor(model => model.Email) @Html.EditorFor(model => model.Email)
    @Html.LabelFor(model => model.Telephone) @Html.EditorFor(model => model.Telephone)
    @Html.LabelFor(model => model.Message) @Html.ValidationMessageFor(model => model.Message) @Html.TextAreaFor(model => model.Message)
        }
    }
    

    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:

    Error loading Partial View script (file: ~/Views/MacroPartials/Contact.cshtml)
  • Nick 101 posts 123 karma points
    May 22, 2014 @ 13:59
    Nick
    0

    The formatting is terrible. 

    Much better here 

  • Nick 101 posts 123 karma points
    May 22, 2014 @ 15:22
    Nick
    0

    Okay it works if I write this on the template and the form behaves correctly:

    @Html.Action("RenderContactForm","ContactUs")

    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 

Please Sign in or register to post replies

Write your reply to:

Draft