jvm
jvm

Reputation: 1712

Get value of dropdownlist in MVC

My ModelClass in

Public class Employee
{
    public SelectList PublishMonth { get; set; }
}

On view, it display as dropdown and working fine. Now on another partial view, i need to display all employee. I get List and i need to display value of PublishMonth for each employee. if i display like <%=item.PublishMonth.SelectedValue%> it display ID instead of text. How can i get text?

Upvotes: 0

Views: 152

Answers (1)

Charles Ouellet
Charles Ouellet

Reputation: 6518

I would do something like this:

public class Employe
{
  SelectList PublishedMonths {get;set;}
  int Month {get;set;}
}

in the view:

<%= Html.DropDownListFor(x => x.Month, PublishedMonths) %>

Then when you post the form, the selected value will be binded in the Month property.

When you build your SelectList, I supposed you have something that sets the DisplayValue of the item.

I will suppose it is an extension method on an int

In the view I would do:

<%= Model.Month.GetDisplayValue() %>

Upvotes: 1

Related Questions