Click here to Skip to main content
15,120,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am very new to mvc and this is really a very simple question . I need to bind a dropdownlist selected value from database .The dropdown has only two options ie one is true and false . I have binded the dropdownlist values but Im not able to set the selected value .The webgrid values are populated using stored procedure . Could anyone help me please . Any help will be really appreciated .

What I have tried:

grid.Column("status", "Status", format: @<text>
                                                                                                      <label id="lblStatus">@item.status</label>



                                                                                                  @Html.DropDownList("value", new List<SelectListItem>
                                { new SelectListItem{Text="True", Value="True",Selected=@item.status},
                         new SelectListItem{Text="False", Value="False",Selected=@item.status}},"Select",  new { @class = "form-inlineinput" } )
Posted
Updated 14-Jun-21 6:28am

1 solution

Quote:
C#
new SelectListItem{Text="True", Value="True", Selected = @item.status },
new SelectListItem{Text="False", Value="False", Selected = @item.status }
You're setting both items to selected if the item's status is true, and neither of them to selected if the status is false.

You need to set one item as selected depending on the status:
C#
new SelectListItem{Text="True", Value="True", Selected = item.status },
new SelectListItem{Text="False", Value="False", Selected = !item.status }
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900