简体   繁体   中英

Getting the Value from a Datatype Dropdown (Umbraco)

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

服务下拉列表值

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;

带ID的填充下拉列表

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

You are using preValue for both Text and Value in your list.

Change it to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM