weixin_33720956 2017-11-28 16:36 采纳率: 0%
浏览 33

填充模态项目

I am looking to open and populate a Bootstrap modal, based on the row selected from my HTML table and the data I have got from my model, I have a button within my HTML table that will call a JS function and pass the ID to it. I am able to get the data back, but I just don't know how to populate the model with the JSON list.

This is my modal I want to build dynamically:

 <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Change User Deparment</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="Employee Number" class="form-control-label">Employee Number:</label>
                        <input type="text" class="form-control" id="Employee Number">
                    </div>

                    <div class="form-group">
                        <label for="userName" class="form-control-label">User Name:</label>
                        <input type="text" class="form-control" id="userName">
                    </div>

                    <label for="department">Select Department</label>
                    <select class="form-control" id="department">
                        <option>Option1</option>
                        <option>Option2</option>
                        <option>Option3</option>
                    </select>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Send message</button>
            </div>
        </div>
    </div>
</div>

This My JS function, to get my data from my model.

function editUser(id) {

    //alert(userId);

    $.ajax({
        type: "GET",
        url: '@Url.Action("GetUserDetails", "Admin")',
        contentType: "application/json; charset=utf-8",
        datatype: JSON,
        data: ({ 'Value': id }),
        success: function () {



        },
        error: function () {
            alert(test);
        }
    });
}
  • 写回答

2条回答 默认 最新

  • 撒拉嘿哟木头 2017-11-28 16:42
    关注

    First, you might want to assign names to your input fields in the form in the modal:

            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label for="Employee Number" class="form-control-label">Employee Number:</label>
                        <input type="text" class="form-control" name="EmployeeNumber" id="Employee Number">
                    </div>
    
                    <div class="form-group">
                        <label for="userName" class="form-control-label">User Name:</label>
                        <input type="text" class="form-control" name="UserName" id="userName">
                    </div>
    
                    <label for="department">Select Department</label>
                    <select class="form-control" id="department">
                        <option>Option1</option>
                        <option>Option2</option>
                        <option>Option3</option>
                    </select>
                </form>
            </div>
    

    Then, use jQuery to assign the values based on the names. This may be different based on what your data looks like. Should be something like this:

    $.ajax({
        type: "GET",
        url: '@Url.Action("GetUserDetails", "Admin")',
        contentType: "application/json; charset=utf-8",
        datatype: JSON,
        data: ({ 'Value': id }),
        success: function (result) {
            $('#exampleModal form input[name=EmployeeNumber]').val(result.EmployeeNumber);
            $('#exampleModal form input[name=UserName]').val(result.UserName);
        },
        error: function () {
            alert(test);
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败