Developer forum

Forum » Development » OptionItem in Code First

OptionItem in Code First

Aki Ruuskanen
Aki Ruuskanen
Reply

Hi,

I have an News Item where one field is a Dropdown with values from an another Item.  

The dropdown field in News Item:

        [Name("Kategori")]
        [Field("NewsCategory", typeof(DropDownListEditor<NewsCategory>))]
        [OptionItem("NewsCategory", "Name", "Id", IncludeChilds = true, IncludeParagraphs = true, SourceType = Dynamicweb.Content.Items.Metadata.FieldOptionItemSourceType.CurrentArea)]
        [Required]

 

The class with news categories 

    [Item("Nyhetskategori", "Nyhetskategori"), Category("Nyheter")]
    [AreaRule,
    StructureRule(StructureContextType.Paragraphs),
    ParentRule(ParentRestrictionRule.ParentType.RootOfWebsite, ParentRestrictionRule.ParentType.RegularPage),
    TitlePattern("{{Name}}"),
    TitleField("Name")]
    [CustomizedURLs]

    public class NewsCategory: ItemEntry
    {
        [Name("Namn")]
        [Field("Name", typeof(Dynamicweb.Content.Items.Editors.TextEditor))]       
        [Required]
        public string Name{ get; set;}
    }

The item renders fine in admin with the dropdown field. And the correct values are shown inte the dropdown. 

But when I create a new "News" and save the value for the dropdown is NOT saved. 

Does someone have an idea about what I missed?

Regards / Aki

 

 

 

 

 


Replies

 
Nicolai Pedersen
Reply

Hi Aki

Try providing a default value to the field - and maybe a "Nothing selected" option.

BR Nicolai

 
Aki Ruuskanen
Aki Ruuskanen
Reply

Unfortunately no luck with the default value. 

How do you  mean a "Nothing selected" option? The source for the dropdown is items of type "NewsCategory" . Do you mean I should create a "News Category" that says "Nothing selected"?

        [Name("Kategori")]
        [Field("NewsCategory", typeof(DropDownListEditor<NewsCategory>))]
        [DefaultValue("1")]
        [OptionItem("NewsCategory", "Name", "Id", IncludeChilds = true, IncludeParagraphs = true, SourceType = Dynamicweb.Content.Items.Metadata.FieldOptionItemSourceType.CurrentArea, ValueField = "Id")]
        [Required]
        public string NewsCategory { get; set; }

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply
This post has been marked as an answer

Hi Aki,

I think the problem is the type of editor you have specified.

Try this:

[Field("NewsCategory", typeof(DropDownListEditor<string>))]
[OptionItem("NewsCategory", "Name", "Id", IncludeChilds = true, IncludeParagraphs = true, SourceType = FieldOptionItemSourceType.CurrentArea)]
[Required]
public string NewsCategory { get; set; }

 

Votes for this answer: 1
 
Aki Ruuskanen
Aki Ruuskanen
Reply

Thanks Morten, that was it And makes sense of couse. :)

/Aki

 

You must be logged in to post in the forum