selecting a value in the drop down when page loads

Anjali Agarwal 1,531 Reputation points
2024-01-24T06:13:41.6066667+00:00

I have a table that has drop down inside it. The drop down has values like this : This is Test1 This is Test2 This is Test3 This is Test4 This is Test5 There is another row in the table that says Test1 or Test2 and so on. Please see below image: User's image

in the above table, if the first column says "Test1", I want the drop down to pre select "This is test1". If the first column says "Test3" then I want the drop down to select the text "This is Test3". below is my controller code:

 public async Task<IActionResult> Index()
   {
		
		ViewData["DocumentType"] = new SelectList(docTypes, "Id", "Description");
   }
   

description is basically this:

   This is Test1
This is Test2
This is Test3
This is Test4
This is Test5

this is my view

<table class="table table-bordered">
                    <thead>
                        <tr>
                            
                            <th>FileName</th>
                            <th>Type</th>
                            <th>Record Date</th>
                         
                        </tr>
                 </thead>
				 <tbody>
				  @if (Model != null)
                        {
                            @foreach (var item in Model)
                            {
							<tr>
							  <td>
							   <td>
                                        <label id="lblName_@item.DocumentId">@item.Name</label>
                                   
							  </td>
							  <td>
							  <select class="form-control" style="min-width:150px" id="ddldoc_@item.DocumentId" asp-for="@item.DocumentType" asp-items="ViewBag.DocumentType"></select>
							  </td>
							</tr>
							}
						}
				 
				 
				 </tbody>

How can I select the value of the drop down based on the first column value when page loads.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,777 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,941 Reputation points Microsoft Vendor
    2024-01-24T13:10:50.7133333+00:00

    Hi @Anjali Agarwal

    How can I select the value of the drop down based on the first column value when page loads.

    You can create the SelectList instance in the view page and set the selected value based on the Name property of the DocumentModel (created based on the table columns).
    Refer to the following code:
    Controller:

            public IActionResult Index()
            {
                var docTypes = new List<DocType>()
                {
                    new DocType { Id = 1,Description ="This is Test1"},
                    new DocType { Id = 2,Description ="This is Test2"},
                    new DocType { Id = 3,Description ="This is Test3"},
                    new DocType { Id = 4,Description ="This is Test4"},
                    new DocType { Id = 5,Description ="This is Test5"},
                };
    
    
                var testmodellist = new List<DocumentModel>()
                {
                    new DocumentModel { DocumentId = 1, Name="Test1", Date= DateTime.Now },
                    new DocumentModel { DocumentId = 2, Name="Test2", Date=DateTime.Now},
                    new DocumentModel { DocumentId = 3, Name="Test3", Date=DateTime.Now},
                    new DocumentModel { DocumentId = 4, Name="Test4", Date=DateTime.Now},
                    new DocumentModel { DocumentId = 5, Name="Test5", Date=DateTime.Now},
    
                };                
                //new SelectList(docTypes, "Id", "Description");
                ViewData["DocumentType"] = docTypes; //use viewdata transfer list of DocumentType to View Page 
                return View(testmodellist);
            }
    

    View page:

    @model IEnumerable<WebApplication1.Models.DocumentModel>
    
    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
     
    <table class="table table-bordered">
        <thead>
            <tr>
    
                <th>FileName</th>
                <th>Type</th>
                <th>Record Date</th>
    
            </tr>
        </thead>
        <tbody>
            @if (Model != null)
            {
                @foreach (var item in Model)
                {
                    <tr> 
                        <td>
                            <label id="lblName_@item.DocumentId">@item.Name</label>
    
                        </td>
                        <td>
                            <select class="form-control" style="min-width:150px"
                                    id="ddldoc_@item.DocumentId" asp-for="@item.DocumentType"
                                    asp-items="new SelectList(ViewBag.DocumentType, nameof(DocType.Id),
                                    nameof(DocType.Description), 
                                    ((List<DocType>)ViewBag.DocumentType).Where(c=> c.Description.Contains(item.Name)).FirstOrDefault().Id)"></select>
                        </td>
                        <td><label id="lblName_@item.DocumentId">@item.Date</label></td>
                    </tr>
                }
            }
        </tbody>
    </table>
    
    

    The result as below:
    User's image


    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.
    Best regards,
    Dillion

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.