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
    Nov 08, 2015 @ 17:49
    Mohammad Javed
    0

    Getting the Value from a Datatype Dropdown

    Hi,

    I am trying to get the name or shall i say the value of the dropdown pre value created using the umbraco datatype "dropdown"

    Service Dropdown List

    Now when specifying the "id" it gets the number, how do i get the value of that "id"?

    Here is the dropdown populated with the id of the values;

    enter image description here

    How do i get the value name? And not the ID?

    Here is the Surface Controller;

    using System;
    using System.Collections.Generic;
    using System.Linq;
    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 = preValue,
                        Value = preValue
                    });
                    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);
    
                return RedirectToCurrentUmbracoPage();
            }
        }
    }
    

    Thanks

    Javed

  • Casper Andersen 126 posts 508 karma points
    Nov 08, 2015 @ 18:35
    Casper Andersen
    0

    As far as i can see in my code which as i know for a fact you are using as you told me :) the problem is in this section of the code

     while (preValues.MoveNext())
                {
                    string preValue = preValues.Current.Value;
                    ListOfGenders.Add(new SelectListItem
                    {
                        Text = preValue,
                        Value = preValue
                    });
                    myModel.ListOfGenders = ListOfGenders;
                }
    

    you wrote

    string preValue = preValues.Current.GetAttribute("id","");

    where as i wrote

    string preValue = preValues.Current.Value;

  • Mohammad Javed 64 posts 373 karma points
    Nov 08, 2015 @ 18:42
    Mohammad Javed
    0

    Here is the full code that mirror yours except for ListOfGenders is ListOfServices in my case.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    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.Value;
                    ListOfServices.Add(new SelectListItem
                    {
                        Text = preValue,
                        Value = preValue
                    });
                    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", SelectedServicePreValueId);
                newComment.SetValue("contactMessage", model.Message);
    
                Services.ContentService.SaveAndPublishWithStatus(newComment);
    
                return RedirectToCurrentUmbracoPage();
            }
        }
    }
    

    On Compile i get this error:

    Severity    Code    Description Project File    Line
    Error   CS7036  There is no argument given that corresponds to the required formal parameter 'provider' of 'DataTypeService.DataTypeService(IDatabaseUnitOfWorkProvider, RepositoryFactory, ILogger, IEventMessagesFactory)'    LiquidThinker2015   D:\Websites\LiquidThinker2015\App_Code\ContactSurfaceController.cs  44
    

    Stack Trace;

    Server Error in '/' Application.
    
    Compilation Error
    
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 
    
    Compiler Error Message: CS1729: 'Umbraco.Core.Services.DataTypeService' does not contain a constructor that takes 0 arguments
    
    Source Error:
    
    
    Line 42:             var newComment = Services.ContentService.CreateContent(model.Name + " - " + DateTime.Now.ToString("dd/MM/yyyy HH:mm"), CurrentPage.Id, "ContactFormula");
    Line 43: 
    Line 44:             DataTypeService myService = new DataTypeService();
    Line 45:             var SelectedService = myService.GetAllDataTypeDefinitions().First(x => x.Id == 1435);
    Line 46:             int SelectedServicePreValueId = myService.GetPreValuesCollectionByDataTypeId(SelectedService.Id).PreValuesAsDictionary.Where(x => x.Value.Value == model.SelectedService).Select(x => x.Value.Id).First();
    
    Source File: d:\Websites\LiquidThinker2015\App_Code\ContactSurfaceController.cs    Line: 44 
    
    
    Show Detailed Compiler Output:
    
    Show Complete Compilation Source:
    
    
    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.106.0
    

    Thanks

    Javed

  • Mohammad Javed 64 posts 373 karma points
    Nov 09, 2015 @ 09:45
    Mohammad Javed
    101

    Here is the fix which has worked for me, was a simple fix, just needed to sleep on it. I now see where the problem is with the help of someone from StackOverflow.

    I was using preValue for both Text and Value in my list.

    I then changed it to:

    ListOfServices.Add(new SelectListItem
    {
        Text = preValues.Current.Value,
        Value = preValues.Current.GetAttribute("id","")
    });
    

    Woohoo! First ever surface controller milestone completed in Umbraco.

    Massive thanks to Casper, Warren for the support provided on here, twitter and YouTube!

Please Sign in or register to post replies

Write your reply to:

Draft