【发布时间】:2019-03-20 06:35:14
【问题描述】:
我在表单中使用 jquery 转发器来动态地将输入组的一部分添加到表单中。我确实尝试过,但无法将输入绑定到模型,我失明了。
这是我的模型。
public class SuggestionCreateEditViewModel
{
public Guid[] DetectionId { get; set; }
public SuggestionCreateEditRepeatedModel[] Repeated { get; set; }
}
public class SuggestionCreateEditRepeatedModel
{
public Guid To { get; set; }
public string Description { get; set; }
public DateTime Deadline { get; set; }
}
表格,为了简洁起见,我删除了表格的很多部分
<div class="col-lg-9 col-md-9 col-sm-12">
<select asp-for="DetectionId" asp-items="ViewBag.AllDetections" class="m-bootstrap-select m_selectpicker toValidate"
multiple data-actions-box="true" data-width="100%"></select>
</div>
<div class="col-lg-9 col-md-9 col-sm-12 input-group date">
<input name = "Repeated.Deadline" type="text" readonly class="form-control toValidate dtDueDate" />
</div>
<div class="col-lg-12 col-md-12 col-sm-12">
<textarea name = "Repeated.Description" class="form-control toValidate txtSuggestion" type="text" >
</textarea>
</div>
在向表单添加新的重复部分并将其发布到服务器之前,如果我 form.serailizeArray() 它返回如下集合(我相信什么 jquery 表单转发器动态形状名称)
{name: "DetectionId", value: "afca1b82-0455-432e-c780-08d6ac38b012"}
{name: "[0][Repeated.To][]", value: "b1176b82-1c25-4d13-9283-df2b16735266"}
{name: "[0][Repeated.Deadline]", value: "04/04/2019"}
{name: "[0][Repeated.Description]", value: "<p>test 1</p>"}
{name: "[1][Repeated.To]", value: "188806d8-202a-4787-98a6-8dc060624d93"}
{name: "[1][Repeated.Deadline]", value: "05/04/2019"}
{name: "[1][Repeated.Description]", value: "<p>test 2</p>"}
和我的控制器
[HttpPost]
public IActionResult CreateSuggestion(SuggestionCreateEditViewModel model, IFormFile[] documents)
{...
控制器无法绑定Repeated,仅绑定了DetectionId。我应该如何塑造我的模型来获取数据?
【问题讨论】:
-
我认为您需要做的唯一更改是将名称更改为 Repeated[1].property 形式,其中 [1] 是数组中的索引。在我回答的另一个类似问题上查看 here 应该会有所帮助
-
你使用了哪个 jquery.repeater.js?把完整的前端代码分享给我们。
标签: c# asp.net-core asp.net-core-mvc model-binding