SelectList 导致我得到空值

SelectList is causing me to get null values

在我的 ASP.NET 核心项目中,由于 selectList,我的模型得到了空值。除了我使用 select 列表 select 编辑的下拉列表中的 selection 外,我的所有属性都在我的视图中显示为空。我正在尝试使用标签助手 asp-items 获取下拉列表 select。我想设置 HTML 表单中的所有数据并保存在名为“Student”的同一模型中

我的模型;

public class Student
{
    public string Name { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string branch { get; set; }

}

我的控制器和我发送的线路 selectList;

public async Task<IActionResult> Signup(Student student) //properties of student come null except branch.
{
    ViewBag.branches = new List<string>() { "Web Programlama", "Mobil Programlama", "Veri Bilimi", "Yapay Zeka/Makine Öğrenmesi", "Genel Tavsiye" };
    return View();      
}
        

和我的 HTML 行,我称之为 selectedList;

<select asp-for="student.branch" class="form-control" id="alan" asp-items="@(new SelectList(ViewBag.branches))"> //here is where ı stuck :(
<option disabled selected title="Alanınızı seçiniz">Alanınızı seçiniz</option>
</select>

首先,您需要在<select></select>中添加name="branch"。然后,您需要add value to option

代码

<select asp-for="student.branch" class="form-control" id="alan" asp-items="@(new SelectList(ViewBag.branches))"> //here is where ı stuck :(
<option disabled selected title="Alanınızı seçiniz">Alanınızı seçiniz</option>
</select>

在html中是这样的,选项没有值:

这是一个工作演示:

查看:

<form method="post">
        
        <select asp-for="student.branch" name="branch" class="form-control" id="alan">
            //here is where ı stuck :(
            <option disabled selected title="Alanınızı seçiniz">Alanınızı seçiniz</option>
            @foreach (var branch in @ViewBag.branches)
            {
                <option value=@branch>@branch</option>
            }
        </select>
        <button type="submit">submit</button>
    </form>

结果: