IEnumerable<SelectListItem> 作为 NULL 返回

时间:2021-01-08 21:59:10

标签: c# asp.net asp.net-core

我有一个 MVC 应用程序用作概念验证,但在将我们设计的一部分实施到其中时遇到问题。

我有 TaxRate.cs:

RUN echo "$env" >> path/to/angular/file/controller

TaxGroup.cs:

[Key]
public short TaxId { get; set; }
public short LocationId { get; set; }
[Column("TaxRate", TypeName = "decimal(12, 5)")]
[Display(Name = "Rate")]
public decimal TaxRate1 { get; set; }
[Required]
[StringLength(21)]
public string Name { get; set; }
[Required]
[StringLength(100)]
public string Description { get; set; }
[Column(TypeName = "decimal(12, 2)")]
[Display(Name = "Taxed Amount Minimum")]
public decimal AmountMin { get; set; }
[Column(TypeName = "decimal(12, 2)")]
[Display(Name = "Tax Amount Maximum")]
public decimal AmountLimit { get; set; }
public bool IsActive { get; set; }
public int? ModifiedBy { get; set; }
[NotMapped]
public DateTime ValidFrom { get; set; }
[NotMapped]
public DateTime ValidTo { get; set; }

TaxGroupViewModel.cs:

[Key]
public short TaxGroupId { get; set; }
public short LocationId { get; set; }
[Required]
[StringLength(21)]
public string Name { get; set; }
[Required]
[StringLength(100)]
public string Description { get; set; }
public bool IsActive { get; set; }
public int? ModifiedBy { get; set; }
[NotMapped]
public DateTime ValidFrom { get; set; }
[NotMapped]
public DateTime ValidTo { get; set; }

此视图用于将税率从 TaxRates 添加到 AppliedRates。

AddRates.cshtml:

public short TaxGroupId { get; set; }
public short LocationId { get; set; }
public string Name { get; set; }
public IEnumerable<SelectListItem> TaxRates { get; set; }
public IEnumerable<SelectListItem> AppliedRates { get; set; }

我遇到的问题是,当我提交表单时,TaxRates 和 AppliedRates 作为 NULL 返回到控制器方法。做了一些研究,我发现这可能是因为我必须为 TaxRate.cs 创建一个自定义模型绑定。这是唯一的方法还是有其他方法?

编辑:

@model TaxGroupViewModel @{ var loc = ViewData["LocationId"]; } <main> @using (Html.BeginForm("AddRates", "Location", FormMethod.Post)) { <h1>Add taxes to @Model.Name</h1> <input hidden="hidden" asp-for="LocationId" value="@Model.LocationId" /> <input hidden="hidden" asp-for="TaxGroupId" value="@Model.TaxGroupId" /> <input hidden="hidden" asp-for="Name" value="@Model.Name" /> <section class="form-row"> <div class="col-md-6"> <h2>Available Rates</h2> @Html.ListBoxFor(m => m.TaxRates, new SelectList(Model.TaxRates, "Value", "Text"), new { @class = "col-md-6 form-control" }) <br /> <input type="button" id="apply" value="Add" class="col-md-2" /> </div> <div class="col-md-6"> <h2>Applied Rates</h2> @Html.ListBoxFor(m => m.AppliedRates, Enumerable.Empty<SelectListItem>(), new { @class = "col-md-6 form-control" }) <br /> <input type="button" id="remove" value="Remove" class="col-md-2" /> </div> </section> <section class="form-row"> <div class="col-md-12"> <button id="savebtn" type="submit" class="col-md-2 btn btn-primary" onlick="">Save</button> @Html.ActionLink("Cancel", "Edit", "Location", new { locationId = loc }, new { @class = "btn btn-secondary col-md-2" }) </div> </section> } </main> <script> $('#apply').click(function () { inHTML = ""; $("#TaxRates option:selected").each(function () { var optionVal = $(this).val(); var exists = false; $('#AppliedRates option').each(function () { if (this.value == optionVal) { exists = true; } }); if (!exists) { inHTML += '<option value="' + $(this).val() + '">' + $(this).text() + '</option>'; } }); $("#AppliedRates").append(inHTML); var ids = ""; $('#AppliedRates option').each(function () { ids += this.value + ","; }); $('#hidden1').val(ids); }); $('#remove').click(function () { $("#AppliedRates option:selected").remove(); var ids = ""; $('#AppliedRates option').each(function () { ids += $(this).val() + ";"; }); $('#hidden1').val(ids); }); } </script>

AddRates 方法:

LocationId=94&TaxGroupId=12&Name=asdfasd&TaxRates=396&TaxRates=397&TaxRates=398&TaxRates=399&__

1 个答案:

答案 0 :(得分:-2)

在您的 AddRates 控制器中有一个返回视图命令,您应该像这样编辑

TaxGroupViewModel t=new TaxGroupViewModel();
Return view(t.tolist());