Takishima
Takishima

Reputation: 31

get index combobox after selectedindexchanged mvc c#

In ASP.NET, I'm not ablet to get the updated value of the SelectedItem from the ComboBox. I've tried various ways but still could not get the value of the index of the combobox. I hope you can help me to fix my problem. enter image description here

I have a combobox with few items and the default setting selectedindex is 0 (first item). After submitting I go to another form. The issue is, that when submitting the form, the default value is saved as opposed to what I have selected from the list. this is combobox view :

@Html.DevExpress().ComboBoxFor(m => m.NMUNIT, settings =>
{
    settings.Name = "UNIT_ID_CB";
    settings.CallbackRouteValues = new { Controller = "App", Action = "cbPartialCategories" };
    settings.Properties.ValueField = "KDUNIT";
    settings.Properties.TextField = "NMUNIT";
    settings.SelectedIndex = 18;
}).BindList((new eFaktur_Model.Lov.lovCategories().ListKategori() )).GetHtml()

this is partial view to load combobox :

groupItem.Items.Add(item =>
    {
                item.Caption = "Kategori";
                item.CaptionSettings.AssociatedNestedExtensionName = "kategoriFaktur";
                item.SetNestedContent(() =>
                {                
                    @Html.RenderPartial("_cbPartialCategories", Model);            
                });
            });

Upvotes: 0

Views: 387

Answers (1)

Mihir Kale
Mihir Kale

Reputation: 1118

You need to check for the IsPostBack property on the form load event when popuating the combo box (dropdownlist)

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           //Code to populate the Combo Box
        }
    }

The Combo box is reset each time the page is post back hence the above code will avoid doing that. It will populate the Combobox only the first time the page is requested.

Upvotes: 0

Related Questions