Unbreakable Unbreakable - 23 days ago 8
ASP.NET (C#) Question

How to set the value of dropdown coming from model in ASP.NET MVC

I am stuck at "how to set value in the dropdown to the value that is set in the model.

I am adding what I have till now and what I have tried.

So, I have a

PaymentFormMV
Model and it has another Model in it which is
PaymentCCMV


public class PaymentFormMV
{
public PaymentCCMV CreditDetails { get; set; }
public string ModelPropertyPrefixName
{
get
{
return this.ModelPropertyPrefix.Replace("_", ".");
}
}
}
public class PaymentCCMV
{
[Display(Name = "Expiration Date")]
public int ExpirationMonth { get; set; }
}


cshtml file takes data from PaymentFormMV

<div class="form-group">
@Html.LabelFor(model => model.CreditDetails.ExpirationMonth, htmlAttributes: new { @class = "control-label col-md-2 required" })
<div class="col-md-10">
@Html.DropDownList(Model.ModelPropertyPrefixName + "CreditDetails.ExpirationMonth", Helpers.PossibleMonths,
new { id = Model.ModelPropertyPrefix + "CreditDetails_ExpirationMonth", @class = "form-control", style = "width:70px;" })
</div>
</div>


Helpers.PossibleMonths: displays all the months

public static ICollection<SelectListItem> PossibleMonths
{
get
{
return new List<SelectListItem>()
{
new SelectListItem() { Text = "1 - Jan", Value = "1" },
new SelectListItem() { Text = "2 - Feb", Value = "2" },
new SelectListItem() { Text = "3 - Mar", Value = "3" },
new SelectListItem() { Text = "4 - Apr", Value = "4" },
new SelectListItem() { Text = "5 - May", Value = "5" },
new SelectListItem() { Text = "6 - Jun", Value = "6" },
new SelectListItem() { Text = "7 - Jul", Value = "7" },
new SelectListItem() { Text = "8 - Aug", Value = "8" },
new SelectListItem() { Text = "9 - Sep", Value = "9" },
new SelectListItem() { Text = "10 - Oct", Value = "10" },
new SelectListItem() { Text = "11 - Nov", Value = "11" },
new SelectListItem() { Text = "12 - Dec", Value = "12" },
};
}
}


Points:

I am getting integer value
model.CreditDetails.ExpirationMonth = 2
(as per DB)

But I don't know how to set it to the drop down. So when the screen loads I want that value to be tied to the dropdown. Please guide me. SO, if the value is 2 I should get something like
2 - Feb
in the Dropdown UI as mentioned in the Helper Method. I am a beginner so I might not be following the best practice so bear with me on this.

Answer

I second @Vivien's response, but I would recommend to use the overload that takes the selected value (See MSDN:), which you can then set. To then allow passing in the default, I would also recommend using a static method, and allowing the month to be passed in:

public static SelectList PossibleMonths(string defaultValue)
{
        return new SelectList(List<SelectListItem>()
        {
            new SelectListItem() { Text = "1 - Jan", Value = "1" },
            new SelectListItem() { Text = "2 - Feb", Value = "2" },
            new SelectListItem() { Text = "3 - Mar", Value = "3" },
            new SelectListItem() { Text = "4 - Apr", Value = "4" },
            new SelectListItem() { Text = "5 - May", Value = "5" },
            new SelectListItem() { Text = "6 - Jun", Value = "6" },
            new SelectListItem() { Text = "7 - Jul", Value = "7" },
            new SelectListItem() { Text = "8 - Aug", Value = "8" },
            new SelectListItem() { Text = "9 - Sep", Value = "9" },
            new SelectListItem() { Text = "10 - Oct", Value = "10" },
            new SelectListItem() { Text = "11 - Nov", Value = "11" },
            new SelectListItem() { Text = "12 - Dec", Value = "12" },
        }, "Value", "Text", defaultValue);
}

EDIT: Your dropdown could would then look like

@Html.DropDownList(Model.ModelPropertyPrefixName + "CreditDetails.ExpirationMonth", Helpers.PossibleMonths(Model.CreditDetails.ExpirationMonth),
                new { id = Model.ModelPropertyPrefix + "CreditDetails_ExpirationMonth", @class = "form-control", style = "width:70px;" })

That assumes the CreditDetails object is always not null, otherwise null checking is a must.

EDIT: I was incorrect, apparently Html.DropDownList doesn't support SelectList, so I reverted back to the original approach with a twist for setting the selected value (note same drop down code above for this to work).

public static IEnumerable<SelectListItem> PossibleMonths(string defaultValue)
{
        if (defaultValue == null)
           defaultValue = "";

        return new List<SelectListItem>()
        {
            new SelectListItem() { Text = "1 - Jan", Value = "1", Selected = (defaultValue == "1") },
            new SelectListItem() { Text = "2 - Feb", Value = "2", Selected = (defaultValue == "2") },
            new SelectListItem() { Text = "3 - Mar", Value = "3", Selected = (defaultValue == "3") },
            new SelectListItem() { Text = "4 - Apr", Value = "4", Selected = (defaultValue == "4") },
            new SelectListItem() { Text = "5 - May", Value = "5", Selected = (defaultValue == "5") },
            new SelectListItem() { Text = "6 - Jun", Value = "6", Selected = (defaultValue == "6") },
            new SelectListItem() { Text = "7 - Jul", Value = "7", Selected = (defaultValue == "7") },
            new SelectListItem() { Text = "8 - Aug", Value = "8", Selected = (defaultValue == "8") },
            new SelectListItem() { Text = "9 - Sep", Value = "9", Selected = (defaultValue == "9") },
            new SelectListItem() { Text = "10 - Oct", Value = "10", Selected = (defaultValue == "10") },
            new SelectListItem() { Text = "11 - Nov", Value = "11", Selected = (defaultValue == "11") },
            new SelectListItem() { Text = "12 - Dec", Value = "12", Selected = (defaultValue == "12") },
        };
}