how do you default a dropdown list value in mvc razor using vb.net and c#

AppDev 21 Reputation points
2023-02-07T23:00:30.67+00:00

hello I am trying to set a default value for a dropdownlist in vb.net and in c# mvc razor view

here is how I populate the list

here is how I create the drop list in mvc razor

the data options for my list are I would like to default the dropdownlist to status Open if the user has not

change the the status of the the drop down list

Thanks

ST_ID	STATUS_DESC
 1	    Resolved 
 2	    Open


        Function GET_CODE_STATUS_List()               Dim STATUS_LIST = (From s In db.ST_STATUS_TBL Select New SelectListItem With {.Text = s.STATUS_DESC, .Value = s.STATUS_DESC}).ToList()              Return STATUS_LIST          End Function
In the Razor View

             
                 @Html.LabelFor(Function(model) model.Status, htmlAttributes:=New With {.class = "control-label col-md-2"})
                 
                     @Html.DropDownListFor(Function(model) model.Status, CType(ViewBag.CodeStatusList, List(Of SelectListItem)), "Resolved", htmlAttributes:=New With {.class = "form-control", .id = "DDL_STATUS_ID"})
                    
                     
                     @Html.ValidationMessageFor(Function(model) model.Status, "", New With {.class = "text-danger"})
                 
             

In the MVC Controller

        Function GET_CODE_STATUS_List()


            Dim STATUS_LIST = (From s In db.ST_STATUS_TBL Select New SelectListItem With {.Text = s.STATUS_DESC, .Value = s.STATUS_DESC}).ToList()

            Return STATUS_LIST

        End Function
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,399 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,277 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 25,866 Reputation points Microsoft Vendor
    2023-02-09T07:01:12.0466667+00:00

    Hi @AppDev,

    From your code it looks like you are specifying the DropDownListFor as "Resolved",

    you don't need to specify a default value, just set Selected = true.

    .Selected = True

    Demo

    @ModelType  VBDemo.Status
    @Html.LabelFor(Function(model) model.STATUS_DESC)
    @Html.DropDownListFor(Function(model) model.STATUS_DESC, CType(ViewBag.CodeStatusList, List(Of SelectListItem)))
    @Html.ValidationMessageFor(Function(model) model.STATUS_DESC, "", New With {.class = "text-danger"})
    
    Function Index() As ActionResult
            ViewBag.CodeStatusList = GET_CODE_STATUS_List1()
            Return View()
        End Function
        Function GET_CODE_STATUS_List1()
            Dim STATUS_LIST = (From s In db.status Select New SelectListItem With {
                                                       .Text = s.STATUS_DESC,
                                                       .Value = s.ST_ID.ToString(),
                                                       .Selected = True
                                                       }).ToList()
            Return STATUS_LIST
    
        End Function
    
    Public Class Status
        <Key()>
        Public Property ST_ID() As Integer
        Public Property STATUS_DESC() As String
    
    End Class
    

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,846 Reputation points
    2023-02-08T18:53:43.68+00:00

    just set Selected = true on the item of the list you want to be the default.

    0 comments No comments

  2. AgaveJoe 26,136 Reputation points
    2023-02-09T21:50:12.85+00:00

    But lets say I had a list of 5 or 6 items and I wanted something in the middle as my default would I add a Boolean to the database and set it to true>??

    Your are using a List(Of SelectListItem) to populate options in the HTML select. The SelectListItem Class has a property named "Selected" which is a bool type. Simply set the one of the SelectListItem's Selected property to true.

    SelectListItem(3).Selected = True
    

    Which option to select is up to you and logic that you must design and write. However, usually the value (1, 2, 3, ect) comes from the database if a records exits. If this is a new record then the common approach is showing a "Select" option which means no select has been made.

    0 comments No comments