Get value of dropdownlist in MVC
My ModelClass in
Public class Emp开发者_如何学Goloyee
{
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?
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() %>
精彩评论