0

提交时,联系人表单会向web.config文件中的指定电子邮件地址发送电子邮件。然而,目前它发布了“Selected Services”的ID--我如何获取价值呈现而不是ID?我已经尝试在追加完成后浏览列表。看看有什么可以给我,我找不到价值。从MVC中的下拉列表中获取值

这些值已在Umbraco中使用自定义数据类型定义。

这里是表面控制器;

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); 
    } 
    } 
} 

编辑:

@当我这样做,我得到这个co0ke;

enter image description here

或者我应该尝试传递 “1435”?

回答

0

您可以在名为.GetPreValueAsString(id)的UmbracoHelper上使用该方法,并传递prevalue的id。

UmbracoHelper在SurfaceController继承的PluginController上作为名为'Umbraco'的属性提供。

+0

在图片上面发布了回复 – MJCoder

+0

@MJCoder - GetValueAsString方法的参数需要是一个整数,目前您正试图传递一个字符串。这里是关于如何将字符串转换为int的一些信息http://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int – co0ke