如何生成表单

时间:2017-05-21 19:05:46

标签: c# jquery ajax asp.net-mvc razor

当我点击 - &gt;时,如何使用MVC C#和ajax复制以下表单? <input type="submit" value="Add another form" class="btn btn-default" />

例如,如果我点击一个按钮5次,我想要生成5个表单。

我希望用户添加更多客户端,而不是通过插入客户端信息来循环,然后再次使用相同的过程提交。

表格重复 - &gt;

@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    {
        <div class="form-group">
            @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Phone_Number, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Phone_Number, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Phone_Number, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
            </div>
        </div>
        <hr />
        <br />


    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

1 个答案:

答案 0 :(得分:1)

使用lazy val logger = .... .clone()的jquery。您还需要更改输入的ID。

.append()
var inputIndex = 1;

$('.add').click(function(){
  inputIndex = inputIndex + 1;
  var inputIndexStr = inputIndex.toString();
  var inputClone = $('.inputDiv:first-child').clone();
  $(inputClone).find('input').val('').attr('id','input' + inputIndexStr + '');
  $('form').append(inputClone);
});
.add{
  color: red;
  cursor: pointer;
}