【问题标题】:Razor dynamically adding row to table inside of bootstrap modalRazor 在引导模式内动态地将行添加到表中
【发布时间】:2019-07-15 15:37:31
【问题描述】:

我有一个带有动态按钮的表格,可以显示带有信息的模式。在模式中,当我单击按钮时,我想在表格中添加一个新行。

我从我的主视图中显示模态,然后调用一个部分来显示模态表中的信息。

在我尝试使用 append 但似乎不起作用的部分中,我认为 jQuery 可以添加到表中,并且我不必调用任何类型的刷新。

我的问题是:如何从部分内部刷新我的表格?

我相信 jQuery 正在添加到 td 但它只是没有更新

EditApplication.cshmtl主视图

<form method="POST">
   <div class="modal fade" id="editAppModal" tabindex="-1" role="dialog" aria-labelledby="editAppModal" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">

      <div class="modal-header">
        <h5 class="modal-title" id="editAppModal">Edit Application</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>

      <div class="modal-body">
          <input type="text" id="appName">
          <input type="text" id="appShortName" style="width:15%">
          <hr>
            <table id="modalServerTB" style="border-collapse: separate; border-spacing: 5px 15px;">

            </table>
      </div>

      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary" asp-page-handler="UpdateApplication">Update Application</button>
      </div>

    </div>
  </div>
</div>
</form>

主视图中的 jQuery

<script>
    $(document).ready(function(){
        $(".selectRow").click(function(e)
        {
            e.preventDefault();

            var row = $(this).closest("tr"),      
                tds = row.find("td:nth-child(1)").first(); //get row closest to click then grab first column info

            var textval = $.trim(tds.text()); //tons of whitespace around the text, needs to be removed

            $.ajax({
                    url: "EditApplication?handler=GetRowInfo",
                    type: "POST",
                    dataType: "json",
                    data: { textval },
                    beforeSend: function (xhr) {
                        xhr.setRequestHeader("XSRF-TOKEN",
                            $('input:hidden[name="__RequestVerificationToken"]').val());
                    },
                    complete: function (result) {
                        $("#appName").val(textval);
                        $("#appShortName").val(result.responseJSON);
                        $('#modalServerTB').load('/EditApplication?handler=ServerPartial');
                        $('#editAppModal').modal('show');

                    }
            })
        });
    });
</script>

partial view

    <tbody id="tableBody">
            @foreach(var s in Model.serverUrl)
            {
                <tr style="border-bottom: 0.3px outset whitesmoke" >

                    <td style="width:40%">
                        <input type="text" value="@s.Key">                
                    </td>

                    <td>
                        @foreach(var u in s.Value)
                        {
                            @Html.DropDownListFor(a=>u.Urlid, Model.urls, new{style = "width:50%", @class = "selectedURL"})
                            <br>                          
                        }
                    </td> 

                    <td class="newURLS" style="border:solid 1px">

                    </td>

                    <button class="AddURLBtn"> + </button>
                    <br>
                </tr>
            }           
    </tbody>

    <div>
        <button type="button" class="btn btn-dark btn-sm" id="AddServerBtn">Add Server </button>
    </div>

    @Html.DropDownList("selectedURL",Model.urls, "Select URL" , new {style = "width:50%; display:none;", @class = "selectedURL", id="urlDDL"})

<script>
$(document).ready(function(){
    $(".AddURLBtn").click(function(e)
        {
            e.preventDefault();

            console.log("inaddurl");
            $(".newURLS").append('sgagfsafsafsf');
        });
});
</script>

截至目前,我只是在测试是否可以将文本添加到 td,但它似乎不起作用

我还将视图模型传递给我的部分

【问题讨论】:

    标签: c# jquery razor


    【解决方案1】:

    事实证明,您需要一个表格标签才能完成所有工作。只有 TBody 没有创建 div,因此我的 jquery 函数没有目标

    <table>
            <tbody id="tableBody">
                @foreach(var s in Model.serverUrl)
                {
                    <tr style="border-bottom: 0.3px outset whitesmoke" >
    
                        <td style="width:40%">
                            <input type="text" value="@s.Key">                
                        </td>
    
                        <td class="urlsTD">
                            @foreach(var u in s.Value)
                            {
                                @Html.DropDownListFor(a=>u.Urlid, Model.urls, new{style = "width:50%", @class = "selectedURL"})
                                <br>                          
                            }
    
                        </td> 
    
    
    
                        <td>
                            <button class="AddURLBtn"> + </button>
                        </td>
    
                    </tr>
                }           
        </tbody>
    
        <div>
            <button type="button" class="btn btn-dark btn-sm" id="AddServerBtn">Add Server </button>
        </div>
    </table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      相关资源
      最近更新 更多