Html.DropDownListFor() doesn't always render selected value

Posted by Andrey on Stack Overflow See other posts from Stack Overflow or by Andrey
Published on 2010-06-01T09:31:03Z Indexed on 2010/06/01 9:33 UTC
Read the original article Hit count: 3040

I have a view model:

public class LanguagesViewModel
{
    public IEnumerable<LanguageItem> Languages { get; set; }

    public IEnumerable<SelectListItem> LanguageItems { get; set; }

    public IEnumerable<SelectListItem> LanguageLevelItems { get; set; }
}

public class LanguageItem
{
    public int LanguageId { get; set; }
    public int SpeakingSkillId { get; set; }
    public int WritingSkillId { get; set; }
    public int UnderstandingSkillId { get; set; }

    public LanguagesViewModel Lvm { get; internal set; }
}

It's rendered with the following code:

                <tbody>
                    <% foreach( var language in Model.Languages ) { Html.RenderPartial("LanguageItem", language); } %>
                </tbody>

LanguageItem.ascx:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<HrmCV.Web.ViewModels.LanguageItem>" %>

<tr id="lngRow">
    <td class="a_left">
        <%: Html.DropDownListFor(m => m.LanguageId, Model.Lvm.LanguageItems, null, new { @class = "span-3" }) %>
    </td> 
    <td>
        <%: Html.DropDownListFor(m => m.SpeakingSkillId, Model.Lvm.LanguageLevelItems, null, new { @class = "span-3" }) %>
    </td>
    <td>
        <%: Html.DropDownListFor(m => m.WritingSkillId, Model.Lvm.LanguageLevelItems, null, new { @class = "span-3" })%>
    </td>
    <td>
        <%: Html.DropDownListFor(m => m.UnderstandingSkillId, Model.Lvm.LanguageLevelItems, null, new { @class = "span-3" })%>
    </td>
    <td>
        <div class="btn-primary">
            <a class="btn-primary-l" onclick="DeleteLanguage(this.id)" id="btnDel" href="javascript:void(0)"><%:ViewResources.SharedStrings.BtnDelete%></a>
            <span class="btn-primary-r"></span>
        </div>
    </td>
</tr>

The problem is that upon POST, LanguageId dropdown doesn't render its previously selected value. While all other dropdowns - do render. I cannot see any differences in the implementation between them. What is the reason for such behavior?

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about html.dropdownlistfor