I want to populate select list directly from view(cshtml file) using dependency injection but getting following error......

Atif Shah 1 Reputation point
2022-03-25T09:47:37.81+00:00

186810-screenshot-1.png

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

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,051 Reputation points Microsoft Vendor
    2022-03-28T03:05:57.267+00:00

    Hi @Atif Shah ,

    When set the asp-items attribute, we need to use @( {the c# code} ).

    You can refer the following code to populate the select tag using the service.

    View page:

    @model MVCWebApplication.Models.MyViewModel  
    @using MVCWebApplication.Services   
    @inject IServiceRepository _servicerepo  
    <div class="row">  
        <div class="col-md-4">  
            <form asp-action="SelectIndex">  
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
                <div class="form-group">     
                <label>Lucky Employee</label>  
                <select asp-for="EmployeeId" asp-items="@(await _servicerepo.populateSelectList("proc1", "valuea"))" >  
                    <option>Please select one</option>  
                </select>   
                </div>  
                <div class="form-group">  
                    <input type="submit" value="Create" class="btn btn-primary" />  
                </div>  
            </form>  
        </div>  
    </div>  
    

    ServiceRepository.cs file:

    public interface IServiceRepository  
    {  
        Task<List<SelectListItem>> populateSelectList(string procedure, string firstval);  
    }  
    
    public class ServiceRepository : IServiceRepository  
    {  
        public Task<List<SelectListItem>> populateSelectList(string procedure, string firstval)  
        {  
            return Task.Factory.StartNew(() =>  
            {  
                return new List<SelectListItem>()  
                        {  
                            new SelectListItem() { Value = "aa", Text = "AA_" + procedure },  
                            new SelectListItem() { Value = "bb", Text = "BB_" + procedure },  
                            new SelectListItem() { Value = "cc", Text = "CC_" + procedure }  
                        };  
            });   
        }  
    }  
    

    And the model:

    public class MyViewModel  
    {  
        public string EmployeeId { get; set; }  
        public string Comments { get; set; }   
    }  
    

    The output like this:

    187328-image.png


    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

    0 comments No comments