Copied to clipboard

Flag this post as spam?

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


  • Noor Alam 26 posts 97 karma points
    Aug 31, 2019 @ 09:33
    Noor Alam
    0

    umbraco does not exist in the current context.

    using System; using System.Collections.Generic; using System.Xml.XPath; using System.Web.Mvc; using System.Web.Configuration; using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Core.Services;

    namespace umbracodemo2.Helpers { public class PreValueHelper { private const string APPSETTINGERROR_MESSAGE = "Invalid or missing appSetting, ";

        public List<SelectListItem> GetPreValuesFromDataTypeId(int dataTypeId)
        {
            List<SelectListItem> preValueSelectorList = new List<SelectListItem>();
    
            XPathNodeIterator iterator = umbraco.library.GetPreValues(dataTypeId);
            iterator.MoveNext();
            XPathNodeIterator preValues = iterator.Current.SelectChildren("preValue", "");
    
            while (preValues.MoveNext())
            {
                string preValueIdAsString = preValues.Current.GetAttribute("id", "");
                int preValueId = 0;
                int.TryParse(preValueIdAsString, out preValueId);
                string preValue = preValues.Current.Value;
                preValueSelectorList.Add(new SelectListItem { Value = preValue, Text = preValue });
            }
    
            return preValueSelectorList;
        }
    
        public List<SelectListItem> GetPreValuesFromAppSettingName(string appSettingName)
        {
            int dataTypeId = GetIntFromAppSetting(appSettingName);
            List<SelectListItem> preValues = GetPreValuesFromDataTypeId(dataTypeId);
            return preValues;
        }
    
        private int GetIntFromAppSetting(string appSettingName)
        {
            int intValue = 0;
            string setting = GetStringFromAppSetting(appSettingName);
            if (!int.TryParse(setting, out intValue))
            {
                throw new Exception(APP_SETTING_ERROR_MESSAGE + appSettingName);
            }
            return intValue;
        }
    
        private string GetStringFromAppSetting(string appSettingName)
        {
            string setting = WebConfigurationManager.AppSettings[appSettingName] as string;
            if (String.IsNullOrEmpty(setting))
            {
                throw new Exception(APP_SETTING_ERROR_MESSAGE + appSettingName);
            }
            return setting;
        }
    
    
    }
    

    }

    =======================================================

    In above Prehelper file for dropdown list, I am getting error "The name 'umbraco' does not exist in the current context." Please check below code line

    XPathNodeIterator iterator = umbraco.library.GetPreValues(dataTypeId);

    Since I am using umbraco V8 Please suggest.

  • Steve Megson 151 posts 1022 karma points MVP c-trib
    Aug 31, 2019 @ 13:47
    Steve Megson
    0

    Something like this should work in Umbraco 8:

    using Umbraco.Core.Models;    
    using Umbraco.Core.PropertyEditors;
    using Umbraco.Web.Composing;
    
    return Current.Services.DataTypeService.GetDataType(dataTypeId)
                                           .ConfigurationAs<ValueListConfiguration>()
                                           .Items
                                           .Select(i => new SelectListItem { Value = i.Id.ToString(), Text = i.Value })
                                           .ToList();
    
  • Noor Alam 26 posts 97 karma points
    Aug 31, 2019 @ 18:21
    Noor Alam
    0

    Thanks Steve ... Its working.

    One more thing ... Once page load Dropdownlist binding is done successfully but when i am trying to submitting Dropdownlist data using below mention HandleFormSubmit() function then Dropdownlist value showing null and not saving data for same and other values saved into database. Please suggest.

    ==============================================================

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Umbraco.Web.Mvc; using umbracodemo2.Models; using umbracodemo2.Helpers; using System.Net.Mail;

    namespace umbracodemo2.Controllers { public class ContactController : SurfaceController { private PreValueHelper preValueHelper => new PreValueHelper();

        private const string Partial_View_Path = "~/Views/Partials/SharedLayout/";
        public ActionResult RenderContact()
        {
            ContactModel model = new ContactModel();
            model.Subjects = preValueHelper.GetPreValuesFromAppSettingName("SubjectDropDown");
            model.IsCustomers = preValueHelper.GetPreValuesFromAppSettingName("CustomerDropDown");
    
            return PartialView(string.Format("{0}ContactPartial.cshtml", Partial_View_Path), model);
        }
    
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult HandleFormSubmit(ContactModel model)
        {
            if (ModelState.IsValid)
            {
                var message = Services.ContentService.CreateAndSave(string.Format("{0}-{1}", model.Name, DateTime.Now.ToString()),
                    CurrentPage.Id, "contactContent");
    
                message.SetValue("subject", model.Subjects);
                message.SetValue("username", model.Name);
                message.SetValue("email", model.Email);
                message.SetValue("isCustomer", model.IsCustomers);
                message.SetValue("message", model.Message);
                TempData["ContactUsSuccess"] = true;
                Services.ContentService.SaveAndPublish(message);
                //SendEmail(model);
                return RedirectToCurrentUmbracoPage();
            }
            return CurrentUmbracoPage();
        }
    
    
    }
    

    }

  • Noor Alam 26 posts 97 karma points
    Sep 02, 2019 @ 22:20
    Noor Alam
    0

    Kindly suggest guys... waiting response

  • Steve Megson 151 posts 1022 karma points MVP c-trib
    Sep 03, 2019 @ 12:16
    Steve Megson
    0

    What's in your ContactPartial.cshtml? I assume that ContentModel.Subjects is an IEnumerable<SelectListItem>?

    A <select> in your view will only post back the value of the selected option, so MVC has no way to convert that string to an IEnumerable<SelectListItem> and populate model.Subjects for you.

  • Noor Alam 26 posts 97 karma points
    Sep 03, 2019 @ 14:16
    Noor Alam
    0

    Model Class :-

    public class ContactModel { public int Id { get; set; }

        [Display(Name = "Subject:")]
        [Required(ErrorMessage = "Please choose Subject")]
        public string Subject { get; set; }
        public List<SelectListItem> Subjects { get; set; }
    
    
        [Required(ErrorMessage ="Enter Name")]
        public string Name { get; set; }
    
        [Required(ErrorMessage ="Enter Email")]
        [EmailAddress]
        public string Email { get; set; }
    
    
        [Display(Name = "Customer Type:")]
        [Required(ErrorMessage = "Please choose Customer")]
        public string IsCustomer { get; set; }
        public List<SelectListItem> IsCustomers { get; set; }
    
        [Required(ErrorMessage = "Enter message")]
        public string Message { get; set; }
    
    }
    

    Contact Partial View :-

    @using (Html.BeginUmbracoForm("HandleFormSubmit", "Contact", FormMethod.Post)) {

        @Html.AntiForgeryToken()
        @Html.ValidationSummary();
    
        <div class="field half first">
            @Html.LabelFor(m => m.Subjects)
            @Html.DropDownListFor(m => m.Subject, Model.Subjects, "Please Select...")
            @Html.ValidationMessageFor(m => m.Subject)
        </div>
        <div class="field half first">
            @Html.LabelFor(m => m.Name)
            @Html.TextBoxFor(m => m.Name)
            @Html.ValidationMessageFor(m => m.Name)
        </div>
        <div class="field half">
            @Html.LabelFor(m => m.Email)
            @Html.TextBoxFor(m => m.Email)
            @Html.ValidationMessageFor(m => m.Email)
        </div>
        <div class="field half">
            <div class="field half first">
                @Html.LabelFor(m => m.IsCustomers)
                @Html.DropDownListFor(m => m.IsCustomer, Model.IsCustomers, "Please Select...")
                @Html.ValidationMessageFor(m => m.IsCustomer)
            </div>
        </div>
        <div class="field">
            @Html.LabelFor(m => m.Message)
            @Html.TextAreaFor(m => m.Message)
            @Html.ValidationMessageFor(m => m.Message)
        </div>
        <ul class="actions">
            <li>
                <button>Send Message</button>
            </li>
        </ul>
    }
    
  • Steve Megson 151 posts 1022 karma points MVP c-trib
    Sep 03, 2019 @ 14:55
    Steve Megson
    0

    It looks like you just need to be reading model.Subject rather than model.Subjects.

  • Noor Alam 26 posts 97 karma points
    Sep 03, 2019 @ 15:53
    Noor Alam
    0

    Thanks Steve... its working.

Please Sign in or register to post replies

Write your reply to:

Draft