Dooie
Dooie

Reputation: 1669

MVC Dropdown List

I have a dropdownlist :

<%=Html.DropDownList("memberInvoiceStatus", ViewData["memberInvoiceStatus"] as SelectList)%>

Can I set the default value in the view?

I am binding like this...

IEnumerable<PayStatus> memberInvoiceStatus = new List<PayStatus>(dr.GetPayStatus());
ViewData["memberInvoiceStatus"] = new SelectList(memberInvoiceStatus, "payStatusId", "payStatusText");

So i have a collection of memberInvoiceStatus (4 total) but i need the selected value of each

Upvotes: 0

Views: 982

Answers (3)

Francisco Noriega
Francisco Noriega

Reputation: 14614

A way in which you could solve it is to create a List.

Say your Pay status is something like this:

public class PayStatus
{
   int id;
   string StatusName;
}

so you would do something like this:

IEnumerable<PayStatus> memberInvoiceStatus = new List<PayStatus>(dr.GetPayStatus());

 var statusList = new List<SelectListItem>();

foreach(var status in memberInvoiceStatus)
{
    statusList .Add( new SelectListItem()
    {
       Text = status.StatusName,
       Value = status.id
       Selected = status.StatusName == "MyDefaultStatus" // or some logic that will mark as True the item that you want to be the default selected one.   
    }
}

I guess you could still use it like you are using it, but instead of pasing this:

ViewData["memberInvoiceStatus"] = new SelectList(memberInvoiceStatus, "payStatusId", "payStatusText");

you would pass:

ViewData["memberInvoiceStatus"] = new SelectList(statusList , "payStatusId", "payStatusText");

although I think it is easier if you just added the list of SelectListItem to the viewdata

ViewData["ListOfStatus"] = statusList;

and in the view you do something like this:

<%= Html.DropDownList("Status",ViewData["ListOfStatus"]) %>

that will make a list with the element that you marked as selected ( Selected = true), as the default (selected) item.

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532745

I'm assuming that you want to have the value reflect the current value of the model. If not, you can use the version of the helper that allows you to specify a default, "unselected" option for the list. If so, you probably want to have two different attributes in your model or view data -- I almost always use a strongly-typed view model for this -- one for the property and one for the list of choices. The helper will pick up the default for the select list from the property value, if available.

Ex:

<%= Html.DropDownList( "memberInvoiceStatus", ViewData["memberInvoiceOptions"] as SelectList ) %>

where memberInvoiceStatus is the property (returned on post and containing default on view) and memberInvoiceOptions is the select list. Using a mode (the way I would go) it would look like:

 public class InvoiceViewModel
 {
      public int MemberInvoiceStatus { get; set; }
      public IEnumerable<SelectListItem> MemberInvoiceOptions { get; set; }
      ...
 }


 <%= Html.DropDownList( "MemberInvoiceStatus", Model.MemberInvoiceOptions ) %>

Using strongly-typed HtmlHelper extensions is probably even better, but requires using the MvcFutures assembly or waiting until MVC 2.0.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

Try this:

<%=Html.DropDownList("memberInvoiceStatus", ViewData["memberInvoiceStatus"] as SelectList, "--DEFAULT TEXT--")%>

Upvotes: 3

Related Questions