【发布时间】:2019-02-06 14:08:23
【问题描述】:
我有一个用于将团队注册到比赛的网络应用程序,每个团队都可以在其中选择他们将用于他们的项目的许多技术。这些技术保存在 Label 类中。
我正在使用视图模型将信息从表单绑定到操作。 但是,当我尝试提交表单时,它会占用除技术列表之外的所有其他字段。
Label.cs
public class Label
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string ColorPalette { get; set; }
}
CreateTeamViewModel.cs
public class CreateTeamViewModel
{
[Required]
public string TeamName { get; set; }
public string ProjectName { get; set; }
public string ProjectDescription { get; set; }
[Required]
public string RepositoryLink { get; set; }
public List<Label> Labels = new List<Label>();
}
TeamsController.cs
public class TeamsController
{
private readonly ApplicationDbContext context;
public IActionResult Create()
{
ViewData["Labels"] = this.context.Labels.ToList();
return View();
}
[HttpPost]
public IActionResult Create(CreateTeamViewModel team)
{
List<Label> labels = team.Labels;
int count = labels.Count; // count = 0
return LocalRedirect("/");
}
}
Create.cshtml(复选框列表)
@model Competition.Data.ViewModels.CreateTeamViewModel
@{
List<Label> labels = ViewData["Labels"] as List<Label>;
}
<form asp-action="Create">
<div class="form-check">
@for(int i = 0; i < labels.Count; i++)
{
<input asp-for="@Model.Labels[i].IsSelected" type="checkbox" />
<label asp-for="@Model.Labels[i].Name">
<span class="badge badge-@labels[i].ColorPalette">@labels[i].Name</span>
</label>
<input asp-for="@Model.Labels[i].Name" type="hidden" value="@labels[i].Name" />
<input asp-for="@Model.Labels[i].ColorPalette" type="hidden" value="@labels[i].ColorPalette" />
}
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
【问题讨论】:
标签: c# razor asp.net-core