Copied to clipboard

Flag this post as spam?

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


  • Alan Mac Kenna 147 posts 405 karma points MVP c-trib
    Sep 10, 2013 @ 18:08
    Alan Mac Kenna
    0

    Access Document Type Properties from MVC Controller

    Hi,

    I have a Contact Form that when submitted I want the Controller Action to look at a property of the current doc type (the contact form doctype) and get a property (the email address to send from) to create the Mail object.

    I'm stuck on how to do this. Any suggesstions?

    I don't want to pass this in as a Route Value as it is an ajax form on the front end and the Route Value properties would be abailable in the html source.

    Cheers :)

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 10, 2013 @ 18:14
    Jeroen Breuer
    0

    Hello,

    Do you post to a Surface Controller? In there you have the CurrentPage property in which you can acces properties of your current node.

    For example:

    CurrentPage.GetPropertyValue<string>("title")

    Jeroen

  • Alan Mac Kenna 147 posts 405 karma points MVP c-trib
    Sep 16, 2013 @ 16:41
    Alan Mac Kenna
    0

    Hi Jeroen thanks for getting back to me.

    I am posting to a Surface Controller. I tried your solution and the error I'm getting back is:

    Cannot find the Umbraco route definition in the route values, the request must be made in the context of an Umbraco request

    My Controller is declared like so:

        public class ContactSurfaceController : Umbraco.Web.Mvc.SurfaceController
    
        {
    
            [HttpPost]
    
            public ActionResult SendMail(Contact form)
    
            {
    
                var test = CurrentPage.GetPropertyValue<string>("myTestProperty");
    
            }
    
       }
    

    I'm rendering the form in a partial like this

    @using (@Ajax.BeginForm("SendMail", new { controller = "ContactSurface" }, ajaxOptions: new AjaxOptions
    
    {
    
        HttpMethod = "POST", 
    
        InsertionMode = InsertionMode.Replace, UpdateTargetId = "target", 
    
        OnFailure = "ShowError()", OnSuccess = "ShowSuccess()" }, htmlAttributes: new { @class="form-horizontal" }))
    
    {
    

    Any idea why I can't access the property?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 16, 2013 @ 16:56
  • Alan Mac Kenna 147 posts 405 karma points MVP c-trib
    Sep 16, 2013 @ 20:18
    Alan Mac Kenna
    0

    Hi Jeroen,

    I wasn't able to get to the bottom of this the way I would have liked. My issue does not seem to be down to the Umbraco behaviour noted in your first link. I think that the root of is might be in me using @Ajax.BeginForm instead of @BeginUmbracoForm. But for my purposes I wanted to use @Ajax.BeginForm.

    I have resolved it now by passing in the node id as part of the form post vars. Then within the Controller I grab the Umbraco node of that ID and query the property that I want to get:

     @using(@Ajax.BeginForm("SendMail", "Contact", new {nid=UmbracoContext.PageId},ajaxOptions: new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "target", OnFailure = "ShowError()", OnSuccess = "ShowSuccess()" }, htmlAttributes: new { @class="form-horizontal" }))
        {
           ... 
         }
    

    And then in the Controller:

        umbraco.NodeFactory.Node contactNode = new umbraco.NodeFactory.Node(Convert.ToInt32(nid));
        string companyEmail = Convert.ToString(contactNode.GetProperty("myProperty"));
    

    I know it's not ideal but it gets me over this hump!

    Thanks again,

    Alan

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 17, 2013 @ 12:48
    Jeroen Breuer
    100

    It's probably because BeginUmbracoForm has a special token which the default Ajax.BeginForm doesn't have. Maybe this issue can help: http://issues.umbraco.org/issue/U4-1458

    Jeroen

  • Alan Mac Kenna 147 posts 405 karma points MVP c-trib
    Sep 17, 2013 @ 13:23
    Alan Mac Kenna
    0

    That could be it alright :)

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 17, 2013 @ 13:28
    Jeroen Breuer
    0

    Hello,

    Could you mark a post as resolved if it helped you?

    Jeroen

  • John Churchley 27 posts 172 karma points
    Oct 19, 2013 @ 23:57
    John Churchley
    0

    Don't forget to re-add/not-delete the two keys below in the Web.config if your using @Ajax.BeginForm when upgrading. Took hours to realise my mistake!

     <add key="ClientValidationEnabled" value="true"/>

    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>

  • Alan Mac Kenna 147 posts 405 karma points MVP c-trib
    Oct 20, 2013 @ 01:24
    Alan Mac Kenna
    0

    thanks John :)

Please Sign in or register to post replies

Write your reply to:

Draft