我正在用 ASP.NET Core MVC 编写“课程”应用程序。当我尝试应用任何课程时,它不会返回任何值SelectedCourse
。我试过
<input id "mvc1" type="radio" asp-for="SelectedCourse"value="ASP.NET Core MVC" checked="true" />
但它不起作用。
Apply.cshtml
:
<div>
<fieldset></fieldset>
<legend>Select One(1) Course</legend>
<p>
<input id "mvc1" type="radio" asp-for="SelectedCourse"value="ASP.NET Core MVC" checked="true" />
<label>ASP.NET Core MVC</label>
</p>
<p>
<input id "mvc2" type="radio" asp-for="SelectedCourse" value="Blazor" />
<label>Blazor</label>
</p>
<p>
<input id "mvc3" type="radio" asp-for="SelectedCourse"value="API" />
<label>Api</label>
</p>
</div>
Repository.cs
:
namespace BTK_Akademi.Models
{
public static class Repository
{
private static List<Candidate> applications = new();
//Listenin dışardan düzenlenmemesi lazım yani readonly olacak
public static IEnumerable<Candidate> Applications => applications;
public static void Add(Candidate candidate)
{
applications.Add(candidate);
}
}
}
根据您共享的代码,这可能是由于视图中的模型初始化。如果
asp-for="SelectedCourse"
在加载视图时尚未定义,则在提交表单时它可能不会与控制器中的Apply(Candidate candidate)
of[HttpPost]
方法绑定,这可能会导致数据为空。此外,请记住它应该在
<form asp-action="Apply" method="post">
您所缺少的地方。所以,基本上你的模型还没有被相应地绑定。
让我们看看,你应该如何正确地做到这一点:
模型:
加载视图:
看法:
操作方法表单提交:
输出:
您的
Apply.cshtml
文件中存在语法错误。将单选按钮输入更改id "mvc1"
为。id="mvc1"