Take the 2-minute tour ×

I got the following Model:

public class ViewBloqueioNotaFiscal

   public ViewComboStatus ComboStatus = new ViewComboStatus();

   public class ViewComboStatus
      public int? IdStatusSelecionado { get; set; }
      public IEnumerable<SelectListItem> ComboStatus { get; set; }

The following controller method:

public ViewBloqueioNotaFiscal.ViewComboStatus geraComboStatus(int? statusSelecionado)
   ViewBloqueioNotaFiscal.ViewComboStatus combo = new ViewBloqueioNotaFiscal.ViewComboStatus
       IdStatusSelecionado = statusSelecionado,     
       ComboStatus = new[]{
                     new SelectListItem { Value = 1, Text = "Op1"},
                     new SelectListItem { Value = 2, Text = "Op2"}
   return combo;

And my aspx is like:

<%: Html.DropDownListFor(x => x.ComboStatus.IdStatusSelecionado, Model.ComboStatus.ComboStatus) %>

Its getting perfectly displayed for selection but when I submit my form, my post method from controller gets the model perfectly with the values except for this combo that Im recieving null value into the model. As its the first one that I try, I think that something is wrong.

Could you guys check that for me? If you have any better solution for this I d like to know too.

thanks for the help !

share|improve this question
Values come only when they are selected. Are you sure something is selected out of the combobox while u r submitting by post method? –  Birey Oct 19 '11 at 17:26
Yeah.. I just got the two options.. –  Dan-SP Oct 19 '11 at 17:27
add comment

1 Answer

up vote 2 down vote accepted

You are not binding to the correct property of your view model. You are binding to some complex object (ComboStatus) which doesn't make sense.

You should bind the drop down list to the IdStatusSelecionado property:

<%: Html.DropDownListFor(
    x => x.ComboStatus.IdStatusSelecionado, 
) %>

A strongly typed DropDownListFor helper requires at least 2 things on your view model:

  1. A scalar property (int, decimal, string, ...) which will be used to bind to
  2. A collection of value/text pairs.

If the collection of value/text pairs contains an item whose value is equal to the scalar property you used as first argument, this item will be preselected. For example if you wanted to preselect the second item in your example you would set IdStatusSelecionado=2 on your view model.

Side note: Model.ComboStatus.ComboStatus looks terrible. Please rename.

share|improve this answer
thanks for the help,bro ! Ill rename it. Anyway, I just forgot to put the IdStatusSelecionado porperty here, but my code is with that. And I have the same problem ( even with x => x.ComboStatus.IdStatusSelecionado..) –  Dan-SP Oct 19 '11 at 17:33
@Dan-SP, this means that the value of the IdStatusSelecionado is not found as value in the ComboStatus collection. You may also try the following: ComboStatus = new SelectList(new[] { new SelectListItem { Value = 1, Text = "Op1"}, new SelectListItem { Value = 2, Text = "Op2" }, "Value", "Text", statusSelecionado) when assigning the ComboStatus property in your action. –  Darin Dimitrov Oct 19 '11 at 17:34
Reading your update.. sec.. –  Dan-SP Oct 19 '11 at 18:00
add comment

Your Answer


