Class 具有IEnumerable字段的ASP.NET核心MVC post模型
我试图发布带有几个隐藏字段的模型(基本上保存用户输入) 此视图的模型:Class 具有IEnumerable字段的ASP.NET核心MVC post模型,class,post,asp.net-core-mvc,ienumerable,Class,Post,Asp.net Core Mvc,Ienumerable,我试图发布带有几个隐藏字段的模型(基本上保存用户输入) 此视图的模型: public class LoanModel { public LoanCalendarDTO LoanCalendar { get; set; } public EnterLoanParams LoanParams { get; set; } } 我尝试发回的模型: public class EnterLoanParams { [Required] [DataType(DataType.C
public class LoanModel
{
public LoanCalendarDTO LoanCalendar { get; set; }
public EnterLoanParams LoanParams { get; set; }
}
我尝试发回的模型:
public class EnterLoanParams
{
[Required]
[DataType(DataType.Currency)]
public double TotalLoanAmount { get; set; }
[Required]
[RegularExpression(@"([0-9]{0,2}(,{0,1}([0-9]{0,2})))", ErrorMessage = "Please enter in format as xx,xx")]
public string Rate { get; set; }
[Required]
[RegularExpression(@"([0-9]{0,2})", ErrorMessage = "2 digits ")]
public int YearsToPay { get; set; }
[Required] [DataType(DataType.Date)] public DateTime Start { get; set; }
public List<DecreaseModel> Decreases { get; set; } = new List<DecreaseModel>();
}
在视图中,我将它们显示为:
@model Front.Models.LoanModel
@{
ViewBag.Title = "title";
Layout = "_Layout";
}
<form method="post" asp-controller="Home" id="form">
<input class="form-control" name="TotalLoanAmount" type="number" asp-for="LoanParams.TotalLoanAmount" hidden/>
<input class="form-control" name="Rate" type="text" asp-for="LoanParams.Rate" hidden/>
<input class="form-control" name="YearsToPay" type="text" asp-for="LoanParams.YearsToPay" hidden/>
<input class="form-control" name="Start" type="date" asp-for="LoanParams.Start" hidden/>
@for (int index = 0; index < Model.LoanParams.Decreases.Count; index++)
{
@Html.HiddenFor(m => @Model.LoanParams.Decreases[index].Amount)
@Html.HiddenFor(m => @Model.LoanParams.Decreases[index].Start)
@Html.HiddenFor(m => @Model.LoanParams.Decreases[index].End)
@Html.HiddenFor(m => @Model.LoanParams.Decreases[index].DecreaseType)
@Html.HiddenFor(m => @Model.LoanParams.Decreases[index].ID)
}
<button class="btn col-12 btn-success align-content-center btn-lg"
asp-action="Index" asp-controller="Home" type="submit">Return Back</button>
</form>
但在帖子中,我收到了一张空名单:
相关链接:
编辑:视图的工作代码:
@for (int index = 0; index < Model.LoanParams.Decreases.Count; index++)
{
<input class="form-control" name="Decreases[@index].Amount" type="number"
asp-for="LoanParams.Decreases[index].Amount" hidden/>
<input class="form-control" name="Decreases[@index].DecreaseType" type="text"
asp-for="LoanParams.Decreases[index].DecreaseType" hidden/>
<input class="form-control" name="Decreases[@index].Start" type="date"
asp-for="LoanParams.Decreases[index].Start" hidden/>
<input class="form-control" name="Decreases[@index].End" type="date"
asp-for="LoanParams.Decreases[index].End" hidden/>
<input class="form-control" name="Decreases[@index].ID"
asp-for="LoanParams.Decreases[index].ID" hidden/>
}
for(int index=0;index隐藏字段的名称
属性具有LoanParams.
前缀,这意味着它们将希望绑定到名为LoanParams
的模型
您需要使名称
减少[index]。{Prop}
(就像您对其他隐藏字段所做的那样)。
您可以像
[HttpPost]
public IActionResult Index(EnterLoanParams loan)
{
return View(loan);
}
@for (int index = 0; index < Model.LoanParams.Decreases.Count; index++)
{
<input class="form-control" name="Decreases[@index].Amount" type="number"
asp-for="LoanParams.Decreases[index].Amount" hidden/>
<input class="form-control" name="Decreases[@index].DecreaseType" type="text"
asp-for="LoanParams.Decreases[index].DecreaseType" hidden/>
<input class="form-control" name="Decreases[@index].Start" type="date"
asp-for="LoanParams.Decreases[index].Start" hidden/>
<input class="form-control" name="Decreases[@index].End" type="date"
asp-for="LoanParams.Decreases[index].End" hidden/>
<input class="form-control" name="Decreases[@index].ID"
asp-for="LoanParams.Decreases[index].ID" hidden/>
}