.net:向表单添加字段不会触发验证

时间:2017-01-25 08:52:42

标签: c# jquery .net jquery-validate unobtrusive-validation

将字段添加到已由jquery-unobtrusive解析的表单时,我们无法添加更多字段,因为这些字段未包含在验证中。我们缺少什么?

WallViewModel.cs

public class WallViewModel
{
    public int BuildingId { get; set; }
    public List<WallModel> Walls { get; set; }
}

public class WallModel
{
    [HiddenInput(DisplayValue = false)]
    [Display(Name = "#")]
    public int Id { get; set; }

    [Required]
    [Display(Name = "Wall name")]
    public string Name { get; set; }

    [Required]
    [RegularExpression(RegularExpressions.Numbers, ErrorMessage = ValidationErrorMessages.Numbers)]
    [Display(Name = "Surface m\u00B2")]
    public double Area { get; set; }
}

BuildingController.cs

public BuildingController : Controller {
    public async Task<PartialViewResult> CreateWall(int counter)
    {
        return PartialView("Wall/AddWall", new WallModel {Counter = counter, IsUpdated = true});
    }
}

Wall.cshtml

@{var counter = 1}
@using (Html.BeginForm("UpdateWall", "Building", FormMethod.Post, new {id = "editWallsForm" }))
{
<table id="editWallTable" class="table table-striped">
    <tr>
        <th>
            #
        </th>
        <th>
            Wall name
        </th>
        <th>
            Surface (m<sup>2</sup>)
        </th>
    </tr>

    @{
        for (var i = 0; i < Model.Walls.Count; i++)
        {
            Model.Walls[i].Counter = counter;
            var index = Model.Walls[i].Counter - 1;
            <tr id="Wall_@Model.Walls[i].Id">
                <td>
                    @Html.DisplayFor(model => model.Walls[i].Counter)
                    @Html.HiddenFor(model => model.Walls[i].Id, new { Value = Model.Walls[i].Id, Name = "walls[" + index + "]" + ".Id" })
                    @Html.HiddenFor(model => model.Walls[i].IsUpdated, new { Value = Model.Walls[i].IsUpdated, Name = "walls[" + index + "]" + ".IsUpdated", @class = "IsUpdated" })
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Walls[i].Name, new { @class = "form-control input-sm", Name = "walls[" + index + "]" + ".Name"})
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Walls[i].Area, new { @class = "form-control input-sm", Name = "walls[" + index + "]" + ".Area"})

                </td>
            </tr>
            counter++;
        }
    }
</table>
@Html.ValidationSummary()
}
<button id="submitWall">Submit</button>
<script>

    $.validator.unobtrusive.parse($('#editWallsForm'));
    var counter = @counter;
    $('.addWallButton').on('click', function() {
         $.ajax({
            url: addWallUrl
            data: {counter: counter}
        })
        .done(function (response) {
            $('tbody').append(response);
            counter++;

            $('#editWallsForm').validate();
        }.bind(this));
    });

    $('#submitWall').on('click', function() {
        if (this.$editWallsForm.valid()) {

        }
        else {

        }
    })
</script>

0 个答案:

没有答案