Copied to clipboard

Flag this post as spam?

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


  • Peter Laurie 8 posts 27 karma points
    Sep 07, 2013 @ 19:52
    Peter Laurie
    0

    MVC4 Surface Controller - How to get current page document type properties

    Hi,

    Umbraco version 6.1.5, mvc 4. iis 8 - server 2012.

    I have set up Umbraco in Visual Studio with MVC and created a contact form, using a surface controller and Ajax, which work very well, following the blog post by: Saurabh NanduCreating an Ajax enabled Contact Form in Umbraco 6 with ASP.NET MVC 4 and Twitter Bootstrap.

    I have some page properties in my contact page document type for success messages and email address that can be set by the person controlling the umbraco cms.

    I wish to be able to get these into my form through the surface controller or the model so that I can use them in sending the form and displaying a success message that can be put into the cms. AS I said, the Ajax form work brilliantly, but I cannot eem to be able to get the values of these properties from the cms that were put in when the page was created.

    It is set up so that the surface controller, contactcontroller is in the controller folder, and the model in the model folder - the previous set up i had was not ruely mvc4, and I had these in the app_code folder and used tempdata, which did not work, as when I moved away from the page and went back, the success message was STILL there, so thought it best to go down the ajax route.

    Any help on this would be greatly appreciated. I have read that using umbraco context seems to be the approach, but I do not know how to implement this.

    Controller:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MVCUmbraco.Models;
    using System.Net.Mail;
    using System.Net;

    namespace WW.Controllers
    {
      public class ContactController : Umbraco.Web.Mvc.SurfaceController
     {
      [HttpPost]
     public ActionResult SendMail(Contact form)
      {
    string retValue = "There was an error submitting the form, please try again later.";

     if (!ModelState.IsValid)
    {
     return Content(retValue);
     }
    if (ModelState.IsValid)
     {

     // code for sending email here
     try
     {
      client.Send(mail);
      retValue = "Your Request for Contact was submitted successfully. We will contact you shortly.";
     }
     catch (Exception)
     {
      throw;
     }
     }
      }
      return Content(retValue);
     }
        }

    }

     

    Model: straight forward model fields, here is an example:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;

    namespace MVCUmbraco.Models

    {

        public class Contact
      {
     public string Message { get; set; }

        }

    }

    Form code - partail view:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<WWW.Models.Contact>

    <div id="status"></div>

    @using (Ajax.BeginForm("SendMail", "Contact", new AjaxOptions
    {

        HttpMethod = "POST",
     InsertionMode = InsertionMode.Replace,
     UpdateTargetId = "target",
     OnFailure = "ShowError()",
    OnSuccess = "ShowSuccess()"

    }))

    {

    //form code here

    }

    Again, Any help on this would be greatly appreciated. For eample, how can i get the current page properties, like text fields. 

    Thank you in advance for any help, pointers, suggestions or solutions. If you need any more information, let me know. I was originally trying this code, but of course it did not help. in getting the page properties:

    // set properties from cms

    var senderProp = CurrentPage.GetProperty("replyToAddress");
    var recipientProp = CurrentPage.GetProperty("recipientEmailAddress");
    var subjectProp = CurrentPage.GetProperty("emailSubject");
    var thankYouPageProp = CurrentPage.GetProperty("thankYouPageProp");
    var thankYouMessage = CurrentPage.GetProperty("successMessage");

    Kind regards,

    Peter

Please Sign in or register to post replies

Write your reply to:

Draft