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;
}
}
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();
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();
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.
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, ";
}
=======================================================
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.
Something like this should work in Umbraco 8:
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();
}
Kindly suggest guys... waiting response
What's in your
ContactPartial.cshtml
? I assume thatContentModel.Subjects
is anIEnumerable<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 anIEnumerable<SelectListItem>
and populatemodel.Subjects
for you.Model Class :-
public class ContactModel { public int Id { get; set; }
Contact Partial View :-
@using (Html.BeginUmbracoForm("HandleFormSubmit", "Contact", FormMethod.Post)) {
It looks like you just need to be reading
model.Subject
rather thanmodel.Subjects
.Thanks Steve... its working.
is working on a reply...