© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
2 replies
IdoS

❔ @Html.DropDownListFor results in null for selected item

Hi for the following command:
@Html.DropDownListFor(m=>m.Name, new SelectList(Model.Names))

I get null after submitting form for Name. why is that?

    public class IndexModel : PageModel
    {
        private readonly WebApplication1.Data.ServerContext _context;

        public IndexModel(WebApplication1.Data.ServerContext context)
        {
            _context = context;
            Names = _context.TblCountries.Select(x => x.CountryName).OrderBy(c => c).ToList();
        }
        [BindProperty]
        public NewPlayerRegister NewPlayerRegister { get; set; } = new NewPlayerRegister();

        public List<string> Names { get; set; } = default!;

        public string Name { get; set; } = default!;

        public IList<TblCountries> TblCountries { get; set; } = default!;
        public void OnPost()
        {
            if (string.IsNullOrEmpty(this.Name))
            {
                ModelState.AddModelError(string.Empty, "Please select a country.");
                Page();
                return;
            }
            NewPlayerRegister.SetCountry(this.Name);
            if (ModelState.IsValid)
            {
                // Process the form data and perform other necessary actions
                string res = NewPlayerRegister.Name + "<br>" + NewPlayerRegister.Id + "<br>" + NewPlayerRegister.Phone + "<br>" + NewPlayerRegister.CountrySelection;
                

                // Save data to the database using _context
               // _context.NewPlayerRegisters.Add(NewPlayerRegister);
                _context.SaveChangesAsync();

                // or return any appropriate action result
                this.Response.WriteAsync("<p>" + res + "</p>");
            }
            else
            {
                // Return to the page to display form validation error messages
                Page();
            }
        }
    }
    public class IndexModel : PageModel
    {
        private readonly WebApplication1.Data.ServerContext _context;

        public IndexModel(WebApplication1.Data.ServerContext context)
        {
            _context = context;
            Names = _context.TblCountries.Select(x => x.CountryName).OrderBy(c => c).ToList();
        }
        [BindProperty]
        public NewPlayerRegister NewPlayerRegister { get; set; } = new NewPlayerRegister();

        public List<string> Names { get; set; } = default!;

        public string Name { get; set; } = default!;

        public IList<TblCountries> TblCountries { get; set; } = default!;
        public void OnPost()
        {
            if (string.IsNullOrEmpty(this.Name))
            {
                ModelState.AddModelError(string.Empty, "Please select a country.");
                Page();
                return;
            }
            NewPlayerRegister.SetCountry(this.Name);
            if (ModelState.IsValid)
            {
                // Process the form data and perform other necessary actions
                string res = NewPlayerRegister.Name + "<br>" + NewPlayerRegister.Id + "<br>" + NewPlayerRegister.Phone + "<br>" + NewPlayerRegister.CountrySelection;
                

                // Save data to the database using _context
               // _context.NewPlayerRegisters.Add(NewPlayerRegister);
                _context.SaveChangesAsync();

                // or return any appropriate action result
                this.Response.WriteAsync("<p>" + res + "</p>");
            }
            else
            {
                // Return to the page to display form validation error messages
                Page();
            }
        }
    }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ Selecting @Html.DropDownListFor
C#CC# / help
3y ago
WPF: Binding selected item of ComboBox inside ItemsControl
C#CC# / help
15mo ago
❔ ❔ Bind selected items
C#CC# / help
4y ago
Get index of a selected item in a list view
C#CC# / help
3y ago