locked
DropDownListFor in EditorTemplate not selecting value RRS feed

  • Question

  • User1392140794 posted

    Hi,

    I have an editor template for a custom object. Inside that editor template I use a couple of DropDownListFor helpers. In each of them I specify a unique model property (with the pre-selected value) and the select list containing all the select options.

    Example:

    <%=Html.DropDownListFor(m => m.DocumentCategoryType, Model.DocumentCategoryTypeList) %>


    I know that the option values are being populated (from viewing source) and that my Model is passed in with the correct ID value (DocumentCategoryType).

    When the view is rendered, there is no selected item in my dropdown and therefore it defaults to the first (non-selected) value.

    Does anyone have any ideas?


    Thanks.



    Wednesday, December 16, 2009 11:10 AM

Answers

All replies

  • User1904378495 posted

    Is  Model.DocumentCategoryTypeList a SelectList? If so can we see the code for that and your model?

    Wednesday, December 16, 2009 8:20 PM
  • User1392140794 posted

    Is  Model.DocumentCategoryTypeList a SelectList? If so can we see the code for that and your model?


    Here's the model:


      public class FileUploadModel : ModelBase
    
        {
    
            public int DocumentCategoryType { get; set; }
    
            public int DocumentType { get; set; }
    
            public int UploadMode { get; set; }
    
        }
    


    And the Select List hanging off its parent:

    [NonSerialized]
            private List<SelectListItem> _documentCategoryTypeList;
            [XmlIgnore]
            public List<SelectListItem> DocumentCategoryTypeList
            {
                get
                {
                    if (_documentCategoryTypeList == null)
                    {
                        _documentCategoryTypeList = LoadTypeSelectList(Enums.TypeCategory.DocumentCategoryType);
                    }
                    return _documentCategoryTypeList;
                }
            }
    




    Thursday, December 17, 2009 3:26 PM
  • User-2115483147 posted

    Try refer this article, I think that is what you want,

    http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 22, 2009 4:48 AM