Copied to clipboard

Flag this post as spam?

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


  • Mohammad Javed 64 posts 373 karma points
    Dec 17, 2015 @ 09:51
    Mohammad Javed
    0

    Getting the Value from Dropdown List [MVC RAZOR - Surface Controller]

    Hello,

    On submit the contact form sends out an email to the specified email address in the web.config file. However at the moment it is posting the ID of the "Selected Services" - how do i get the value rendering instead of the ID?

    Here is the surface controller;

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Net.Mail;
    using System.Text;
    using System.Web;
    using System.Web.Mvc;
    using System.Xml.XPath;
    using Umbraco.Core.Services;
    using Umbraco.Web.Mvc;
    
    /// <summary>
    /// Summary description for ContactSurfaceController
    /// </summary>
    
    namespace LiquidThinker2015
    {
        public class ContactSurfaceController : SurfaceController
        {
            public object XPathModeIterator { get; private set; }
    
            public ActionResult ShowForm()
            {
                ContactModel myModel = new ContactModel();
                List<SelectListItem> ListOfServices = new List<SelectListItem>();
                XPathNodeIterator iterator = umbraco.library.GetPreValues(1435);
                iterator.MoveNext();
                XPathNodeIterator preValues = iterator.Current.SelectChildren("preValue", "");
                while (preValues.MoveNext())
                {
                    string preValue = preValues.Current.GetAttribute("id", "");
                    ListOfServices.Add(new SelectListItem
                    {
                        Text = preValues.Current.Value,
                        Value = preValues.Current.GetAttribute("id","")
                    });
                    myModel.ListOfServices = ListOfServices;
                }
                return PartialView("ContactForm", myModel);
            }
    
            public ActionResult HandleFormPost(ContactModel model)
            {
                var newComment = Services.ContentService.CreateContent(model.Name + " - " + DateTime.Now.ToString("dd/MM/yyyy HH:mm"), CurrentPage.Id, "ContactFormula");
    
                //DataTypeService myService = new DataTypeService();
                //var SelectedService = myService.GetAllDataTypeDefinitions().First(x => x.Id == 1435);
                //int SelectedServicePreValueId = myService.GetPreValuesCollectionByDataTypeId(SelectedService.Id).PreValuesAsDictionary.Where(x => x.Value.Value == model.SelectedService).Select(x => x.Value.Id).First();
    
    
                newComment.SetValue("contactName", model.Name);
                newComment.SetValue("companyName", model.Company);
                newComment.SetValue("emailFrom", model.Email);
                newComment.SetValue("telephoneNumber", model.Telephone);
                newComment.SetValue("dropdownServices", model.SelectedService);
                newComment.SetValue("contactMessage", model.Message);
    
                Services.ContentService.SaveAndPublishWithStatus(newComment);
    
                //Send out email
                if (ModelState.IsValid)
                {
                    var sb = new StringBuilder();
    
                    sb.AppendFormat("<p>Name: {0}</p>", model.Name);
                    sb.AppendFormat("<p>Company: {0}</p>", model.Company);
                    sb.AppendFormat("<p>Email: {0}</p>", model.Email);
                    sb.AppendFormat("<p>Phone: {0}</p>", model.Telephone);
                    sb.AppendFormat("<p>Service: {0}</p>", model.SelectedService); //THIS LINE HERE
                    sb.AppendFormat("<p>Message: {0}</p>", model.Message);
    
                    SmtpClient smtp = new SmtpClient();
                    MailMessage message = new MailMessage();
                    MailAddress ma = new MailAddress(model.Email);
    
                    message.Subject = ConfigurationManager.AppSettings["ContactFormSubject"];
                    message.To.Add(new MailAddress(ConfigurationManager.AppSettings["ContactFormTo"]));
                    message.CC.Add(new MailAddress(ConfigurationManager.AppSettings["ContactFormCC"]));
                    message.From = ma;
                    message.Sender = new MailAddress(model.Email);
                    message.Body = sb.ToString();
                    message.IsBodyHtml = true;
    
                    try
                    {
                        smtp.Send(message);
    
                    }
                    catch (SmtpException smtpEx)
                    {
                        // Log or manage your error here, then...
                        return RedirectToUmbracoPage(1084); // Redirect to homepage.
                    }
    
                    return RedirectToUmbracoPage(1454);
                }
    
                return RedirectToUmbracoPage(1454);
            }
        }
    }
    

    Apologies if posted in the wrong section of the forum, couldnt find a section for Surface Controllers.

    Thanks

    Javed

  • Ian 178 posts 752 karma points
    Dec 17, 2015 @ 10:23
    Ian
    0

    Hi Mohammad, I see that you are populating the value of the service list listbox with the service id here

    Value = preValues.Current.GetAttribute("id","")
    

    This is probably to satisfy your contact model.SelectedService may be an int. If this the case you would need to re-map the pre value id back to the string value via

    umbraco.library.GetPreValueAsString(model.SelectedService)
    
  • Mohammad Javed 64 posts 373 karma points
    Dec 17, 2015 @ 10:33
    Mohammad Javed
    0

    Hi Ian,

    So where abouts do i need to change the code?

    So should this line;

    Value = preValues.Current.GetAttribute("id","")
    

    Say;

    umbraco.library.GetPreValueAsString(model.SelectedService)
    

    As part of the send mail script, do i keep it the same so it sames mode.SelectedService?

    Thanks

    Javed

  • Ian 178 posts 752 karma points
    Dec 17, 2015 @ 10:41
    Ian
    0

    this line

    sb.AppendFormat("<p>Service: {0}</p>", model.SelectedService);
    

    would become

    sb.AppendFormat("

    Service: {0}

    ", umbraco.library.GetPreValueAsString(model.SelectedService));

  • Mohammad Javed 64 posts 373 karma points
    Dec 17, 2015 @ 10:45
    Mohammad Javed
    0

    Hi Ian,

    When i do that i get the following error;

    enter image description here

    Cannot convert from 'string' to 'int'

    There shouldnt be a need to convert from string to int though. Eeeek!

Please Sign in or register to post replies

Write your reply to:

Draft