【发布时间】:2020-09-18 11:35:25
【问题描述】:
我想动态添加文本框并将其绑定到我的模型,所以当我发布所有数据时都应该在 HttpPost 中。这是我的模型
public class JobViewModel
{
public JobViewModel()
{
JobDescriptions = new List<JobDescription>();
}
public int Id { get; set; }
[Required]
public string JobTitle { get; set; }
public List<JobDescription> JobDescriptions { get; set; }
}
这是我的 Razor 视图
@using (Html.BeginForm("CreateJob", "Jobs", FormMethod.Post, new { @id = "jobFrm" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Job</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.JobTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.JobTitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.JobTitle, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.Label("Add Description", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-2">
<input id="btnAdd" type="button" class="btn btn-success" value="Add" />
</div>
............. more fields and submit button ............
我搜索并尝试了不同的代码,他们在单击添加按钮时添加了文本框,但填充的文本框值未绑定到我的模型。有时它会添加空模型,或者根本不添加。 需要帮助将值绑定到我的模型
【问题讨论】:
标签: .net asp.net-mvc asp.net-core