首页 > 解决方案 > IEnumerable返回为 NULL

问题描述

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

我有 TaxRate.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; }

TaxGroup.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; }

TaxGroupViewModel.cs:

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。

AddRates.cshtml:

@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>

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

编辑:

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

AddRates 方法:

        [HttpPost]
        public async Task<IActionResult> AddRates(TaxGroupViewModel vm)
        {
            var groups = new List<TaxGrouping>();

            var applied = vm.AppliedRates;

            foreach (var item in applied)
            {
                var grouping = new TaxGrouping()
                {
                    TaxGroupId = vm.TaxGroupId,
                    TaxId = short.Parse(item.Value)
                };

                groups.Add(grouping);
            }

            await _locationRepository.Add(groups);

            return RedirectToAction("Edit", "Location", new { locationId = vm.LocationId });
        }

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

解决方案


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

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


推荐阅读