0

Below is my DropDownList in view

<div class="col-xs-8 col-sm-8 col-md-4">                           
 @Html.DropDownList("Status",new List<SelectListItem> { new SelectListItem{ Text="Active", Value = "Active" },new SelectListItem{ Text="InActive", Value = "InActive" }}, new { @class = "form-control" })
</div> 

From DB value is coming either as "Active" or "Inactive" and dropdown has already these two value. And from my DB i'm assigning value in ViewBag.IsStatus. Now suppose my value is coming "InAactive" from DB then how to assign this as Selected value in Dropdown rather than to show First dropdown by default as selected.

Steve
  • 352
  • 2
  • 6
  • 24
  • The values of your options are `0` and `1` (not `"Active"` and `"Inactive"`) so if the value of property `Status` is `1`, then the second option will be selected. –  Apr 20 '16 at 12:28
  • 1
    Check this answer http://stackoverflow.com/a/6807331/3074022 – Sousuke Apr 20 '16 at 12:30
  • @StephenMuecke ok i have changed my value same as that of Text. but still not got my desired result. – Steve Apr 20 '16 at 12:31
  • Do you have a model property named `Status`? You should always be using the strongly typed `HtmlHelper` methods (`@Html.DropDownListFor(m => m.Status, .....)`) –  Apr 20 '16 at 12:33
  • @Sousuke i'm binding my dropdown in View side which are having only two options. – Steve Apr 20 '16 at 12:38

2 Answers2

1

It's better to use DropDownListFor if you using MVC. But for your case just create SelectList and pass it to DropDownList. SelectList contructor has overload for selected value:

@{ //theese lines actually should be in controller.
 var list = new List<SelectListItem> 
             { 
             new SelectListItem 
                   { 
                       Text="Active", 
                       Value = "0" 
                   }
             ,new SelectListItem
                   { 
                       Text="InActive", 
                       Value = "1" 
                    }
             }
}

//thats your code
<div class="col-xs-8 col-sm-8 col-md-4">                           
   @Html.DropDownList("Status",new SelectList(list, "Value", "Text", ViewBag.IsStatus), new { @class = "form-control" })
</div> 
teo van kot
  • 12,350
  • 10
  • 38
  • 70
  • i have shifted ' var list=' in my controller then how i will access that list in my view inside `SelectList` – Steve Apr 21 '16 at 10:02
  • @Steve but you ctreate this list with the code above? – teo van kot Apr 21 '16 at 10:03
  • @Steve in your controller you should use `ViewData.Model` to store this. And then use Model to get this value. **Or** you can just use `ViewBag.list` and then access to this data with `ViewBag.list` on View – teo van kot Apr 21 '16 at 10:06
  • `public ActionResult GetEditRecord(long Id) { using (StoreProcedureContext sc = new StoreProcedureContext()) { var list = new List { new SelectListItem { Text="Active", Value = "0" } ,new SelectListItem { Text="InActive", Value = "1" } }; ViewBag.List = list; var cmd = sc.EditUsers(Id); ViewBag.IsDeleted = cmd.ToList()[0].IsDeleted; return View(cmd); } }` – Steve Apr 21 '16 at 10:34
  • and my view `@Html.DropDownList("Status", new SelectList(ViewBag.List, "Value", "Text"), new { @class = "form-control" })` please tell me where i'm doing wrong. I want that `ViewBag.IsDeleted` value to selected by default. – Steve Apr 21 '16 at 10:34
  • @Steve and where is `ViewBag.IsDeleted` in your `DropDownList` helper? =) i mean `@Html.DropDownList("Status", new SelectList(ViewBag.List, "Value", "Text", ViewBag.IsDeleted), new { @class = "form-control" })` – teo van kot Apr 21 '16 at 10:52
1

If you have a model with Status property then simply assign this the value to the property (for example in controller):

Model

public class Model
{

    public string Status {get;set;}
} 

Controller

public ActionResult SomeAction()
{
    //the value has to correspond to the Value property of SelectListItems
    //that you use when you create dropdown
    //so if you have new SelectListItem{ Text="Active", Value = "Active" }
    //then the value of Status property should be 'Active' and not a 0
    var model = new Model{Status = "Active"}

    return this.View(model);
}

View:

@model Model


@Html.DropDownListFor(m=>m.Status,new List<SelectListItem> { new SelectListItem{ Text="Active", Value = "Active" },new SelectListItem{ Text="InActive", Value = "InActive" }}, new { @class = "form-control" })
Alex Art.
  • 8,711
  • 3
  • 29
  • 47
  • `@Html.DropDownListFor(m => m.FirstOrDefault().IsDelete, new List { new SelectListItem { Text = "Active", Value = "false" }, new SelectListItem { Text = "InActive", Value = "true" } }, new { @class = "form-control" })` – Steve Apr 20 '16 at 13:06
  • isDelete is my bool property but i'm not able to fetch same as you said for selected value – Steve Apr 20 '16 at 13:07
  • that is to show column name. columns name were not coming but after writing `FirstOrDefault` it was showing columns name for the model – Steve Apr 20 '16 at 13:32
  • @Steve It's actually looks like your model is a collection(or a list) and not a single object... – Alex Art. Apr 20 '16 at 13:33
  • please show the vIew and corresponding controller action code – Alex Art. Apr 20 '16 at 13:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109692/discussion-between-alex-art-and-steve). – Alex Art. Apr 20 '16 at 13:36