如何在ASP.NET Core MVC中将动态添加的jQuery标记绑定到httpost contoller动作?

时间:2018-12-20 08:15:00

标签: asp.net-core-2.1

我正在创建一个车队管理系统。我试图让用户向车辆添加不同的附件,我不确定附件的数量,因此我提供了一个表格,然后用户可以根据需要添加任意数量的附件。

我已经添加了JQuery代码以添加更多行,但是当我发布表单时,控制器操作不会将新添加的字段绑定到模型变量

型号

public class AddVehicleAccessory
{
    public int NumberofAccessories { get; set; }

    public string [] selectedVehicles { get; set; }

    public IList<VehicleAccessory> VehicleAccessories { get; set; }
}

Create.cshtml

@model FleetManagementSystem.Models.Transport.ViewModels.AddVehicleAccessory
 @{
   ViewData["Title"] = "CreateAccessory";
  }

<h2>CreateAccessory</h2>

<div class="row">
<div class="col-md-4">
    <form asp-action="AddCreate">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <table id="myTable" class="table">
            <thead>
                <tr>
                    <th>
                        @Html.DisplayNameFor(model => model.VehicleAccessories[0].AccessoryName)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.VehicleAccessories[0].Accessory_Merchant)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.VehicleAccessories[0].Accessory_Price)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.VehicleAccessories[0].Accessory_Invoice)
                    </th>
                    <th></th>
                </tr>
            </thead>
            <tbody>

                @for (int i = 0; i < Model.NumberofAccessories; i++)
                {
                    //TODO Use this when creating a vehicle accessory page

                    <tr>
                        <td>
                            <select asp-for="VehicleAccessories[i].AccessoryNameID" class="form-control" asp-items="ViewBag.AccessoryID"></select>
                        </td>
                        <td>
                            <input asp-for="VehicleAccessories[i].Accessory_Merchant" class="form-control" />
                            <span asp-validation-for="VehicleAccessories[i].Accessory_Invoice" class="text-danger"></span>
                        </td>
                        <td>
                            <input asp-for="VehicleAccessories[i].Accessory_Price" class="form-control" />
                            <span asp-validation-for="VehicleAccessories[i].Accessory_Price" class="text-danger"></span>
                        </td>

                        <td>
                            <input asp-for="VehicleAccessories[i].Accessory_Invoice" class="form-control" />
                            <span asp-validation-for="VehicleAccessories[i].Accessory_Invoice" class="text-danger"></span>

                        </td>
                    </tr>
                }
        </table>
        <div class="form-group">
            <input type="submit" value="AddCreate" class="btn btn-default" />
        </div>
    </form>
</div>

<button class="btnPlus">Add rows</button>

@section Scripts {
   @Html.Partial("~/Views/Shared/_MyPartial.cshtml", Model);
}

向表中添加更多行的脚本

@model FleetManagementSystem.Models.Transport.ViewModels.AddVehicleAccessory

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
var count = 3;
$(document).ready(function () {
    $(document).on("click", ".btnPlus", function () {

        count++;
        var html =
            '<tr>'+
                '<td>'+
            '<select class="form-control" data-val="true" data-val-required="The AccessoryNameID field is required." id="VehicleAccessories_' + count +'__AccessoryNameID" name="VehicleAccessories[' + count +'].AccessoryNameID"><option value="1">Tow Bar</option>'+
                        '<option value="2">Canopy</option>'+
                        '<option value="3">Bull Bar</option>'+
                        '<option value="4">Lights</option>'+
                        '<option value="5">Radios</option>'+
                        '<option value="6">Branding</option>'+
                    '</select>'+
                '</td>'+
                '<td>'+
            '<input class="form-control" type="text" id="VehicleAccessories_' + count +'__Accessory_Merchant" name="VehicleAccessories['+count+'].Accessory_Merchant" value="" />'+
                    '<span class="text-danger field-validation-valid" data-valmsg-for="VehicleAccessories[' + count +'].Accessory_Invoice" data-valmsg-replace="true"></span>'+
                '</td>'+
                '<td>'+
            '<input class="form-control" type="text" data-val="true" data-val-number="The field Accessory_Price must be a number." data-val-required="The Accessory_Price field is required." id="VehicleAccessories_' + count +'__Accessory_Price" name="VehicleAccessories[' + count +'].Accessory_Price" value="" />'+
                    '<span class="text-danger field-validation-valid" data-valmsg-for="VehicleAccessories[' + count +'].Accessory_Price" data-valmsg-replace="true"></span>'+
                '</td>'+
                '<td>'+
            '<input class="form-control" type="text" id="VehicleAccessories_' + count +'__Accessory_Invoice" name="VehicleAccessories[' + count +'].Accessory_Invoice" value="" />'+
                    '<span class="text-danger field-validation-valid" data-valmsg-for="VehicleAccessories[' + count +'].Accessory_Invoice" data-valmsg-replace="true"></span>'+
                '</td>'+
            '</tr>';
        $("tbody").append($(html).clone(true));
    });
});

控制器动作 问题:发布时动态生成的HTML不会绑定到addVehicleAccesory

[HttpPost]
public IActionResult AddCreate(AddVehicleAccessory addVehicleAccesory){}

1 个答案:

答案 0 :(得分:0)

我找到了答案here

谢谢