【问题标题】:How to generate a form如何生成表格
【发布时间】:2017-05-21 19:05:46
【问题描述】:

点击后如何使用 MVC C# 和 ajax 复制以下表单 --> <input type="submit" value="Add another form" class="btn btn-default" />

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

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

要复制的表格 -->

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

【问题讨论】:

  • 您一次只能提交一个表单,所以这没有任何意义。如果您想动态地向集合中添加项目,我建议您使用 BeginCollectionItem 辅助方法 - 请参阅 this answer 以获取示例

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


【解决方案1】:

使用 jquery 使用 .clone().append() 。您还需要更改输入的 ID。

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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<div class="inputDiv">
  <div>CLIENT NAME</div>
  <input id="input1" type='text'>
</div>
</form>
<div class="add">ADD NEW CLIENT</div>

【讨论】:

  • 您好,如何使用 HtmlHelpers 和 @model List 克隆视图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2014-12-01
  • 2017-11-03
  • 2020-12-27
  • 2010-11-19
  • 2014-07-15
  • 1970-01-01
相关资源
最近更新 更多