c# - trying to set a dropdown in MVC -


I'm almost resolved but need a little push.

What do I have here:

I have an active field in the database which is a little field (True / Folce)

I have used this type of form Dropdown list has been added:

  <% = Html.DropDownList ("lstActive", New SelectList (IEnumerable) ViewData ["ActiveList"]))% & gt;  

In my controller, I just have this code to generate true / wrong in the dropdown:

  list & lt; String & gt; ActiveList = new list & lt; String & gt; (); ActiveList.Add ("this is true"); ActiveList.Add ("wrong"); ViewData ["ActiveList"] = new selection list (active list);  

I want to actively bind to the field in the database and want to select it in the dropdown. When I see it like this I get it:

So the questions are:

Obviously I do not pay attention to the value of the value of the text Is it giving me this issue? And how do I select the value in the database?

Any help would be appreciated.

Here are some suggestions for you.

First of all, the name of your dropdown list is "lstActive", so if you create a list & lt; SelectListItem & gt; Called "LstActive" and pass it back in ViewData, you do not have to do anything with boxing, then your announcement looks:

  <% = html.DropDownList ( "LstActive")%>  

Easy, huh?

In your controller, you create your list here is a method I used to:

Private list & lt; SelectListItem & gt; GetAccounts (user user) {var items = new list & lt; SelectListItem & gt; (); Foreign currency (account account in user account) {var item = new SelectListItem (); Item.Text = account.Name; Item.Value = account.AccountId.ToString (); If (ActiveAccount == account.AccountId) item Selected = true; Items.Add (item); } Return the baggage; }

Actually, the one I am trying to indicate is that you can set a property on your SelectListItem that you want to display as selected. Here, I am using my own code for users and accounts, but you can change your own data based on your DB query.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -