0

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

3 Answers 3

3

Try this:

<%=Html.DropDownList("memberInvoiceStatus", ViewData["memberInvoiceStatus"] as SelectList, "--DEFAULT TEXT--")%>
Sign up to request clarification or add additional context in comments.

2 Comments

That sets a default value, yes, but basically it's a "no option selected" value. That can be handy and is usually the way to go. I was assuming that he wanted it to take a default value from the model -- say in an edit view instead of a new view.
@tvanfosson, there's not enough information in the question. I am just guessing here.
0

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.