❔ @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();
}
}
}
2 Replies
TheRanger
TheRanger17mo ago
u probably need to put [BindProperty] on the Property Name
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server