Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use bootstrap modal to edit the table data in MVC?

I have a table in MVC view that displays employee details. I'd like to add an edit functionality, but instead of opening it in a new page, I'd like to show it using a bootstrap modal. (http://twitter.github.com/bootstrap/javascript.html#modals)

I don't think I have to use ajax since the data is already available on the page. I think I need to some jquery or razor code to pass the selected employee's data to the bootstrap modal, and pop it up on the same screen. Below is my code. Any help would be greatly appreciated. Thanks

@Foreach(var item in Model.Employees)
{
<tr>
   <td>@User.Identity.Name
            </td>
            <td>@item.FirstName
            </td>....other columns
<td><a href="#myModal" role="button" class="btn" data-toggle="modal">Edit</a>
    <td>
    </tr>........other rows
}
**Bootstrap Modal**


<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">

  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Edit Employee</h3>
  </div>

  <div class="modal-body">
    <p>Selected Employee details go here with textbox, dropdown, etc...</p>
  </div>

  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button class="btn btn-primary">Save changes</button>
  </div>
</div>
like image 283
Ren Avatar asked Mar 31 '13 10:03

Ren


People also ask

How can I get Bootstrap table row values on clicking Bootstrap modal?

One approach would be to refresh the content of the modal every time a selection on a row happens. The selection can be handled when clicking the row and the refresh of the (possibly rich/complicated) content of the modal could be achieved by assigning a template to it and binding its rendering to a property.

How do I use Bootstrap modals?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.

How do I get data from modal popup?

Data can be passed to the modal body from the HTML document which gets displayed when the modal pops up. To pass data into the modal body jquery methods are used. jQuery is similar to JavaScript, however jQuery methods are simple and easier to implement. jQuery reduces the lines of code.

How do I change Bootstrap modals?

Bootstrap 4 Modal Change the size of the modal by adding the . modal-sm class for small modals, . modal-lg class for large modals, or . modal-xl for extra large modals.


1 Answers

There are indeed 2 possibilities: with or without AJAX. If you want to do that without AJAX you could subscribe to the click event of the Edit link and then copy the values from the table to the modal and finally show the modal.

So start by giving your edit link some class:

<a href="#" class="btn edit">Edit</a>

that you could subscribe to:

$('a.edit').on('click', function() {
    var myModal = $('#myModal');

    // now get the values from the table
    var firstName = $(this).closest('tr').find('td.firstName').html();
    var lastName = $(this).closest('tr').find('td.lastName').html();
    ....

    // and set them in the modal:
    $('.firstName', myModal).val(firstName);
    $('.lastNameName', myModal).val(lastName);
    ....

    // and finally show the modal
    myModal.modal({ show: true });

    return false;
});

This assumes that you have given proper CSS classes to the <td> elements and the input fields in your modal.


If you wanted to use AJAX you could generate the link like that:

@Html.ActionLink("Edit", "Edit", "Employees", new { id = employee.Id }, new { @class = "btn edit" })

and then you subscribe to the click event of this button and trigger the AJAX request:

$('a.edit').on('click', function() {
    $.ajax({
        url: this.href,
        type: 'GET',
        cache: false,
        success: function(result) {
            $('#myModal').html(result).find('.modal').modal({
                show: true
            });
        }
    });

    return false;
});

you will have a simple placeholder for the modal in your main view that will harbor the details:

<div id="myModal"></div>

The controller action that will be hit should fetch the employee record using the id an dpass it to a partial view:

public ActionResult Edit(int id)
{
    Employee employee = repository.Get(id);
    EmployeeViewModel model = Mapper.Map<Employee, EmployeeViewModel>(employee);
    return PartialView(model);
}

and finally the corresponding partial:

@model EmployeeViewModel

<div class="modal hide fade">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3>Edit Employee</h3>
    </div>
    <div class="modal-body">
        <div>
            @Html.LabelFor(x => x.FirstName)
            @Html.EditorFor(x => x.FirstName)
        </div>
        <div>
            @Html.LabelFor(x => x.LastName)
            @Html.EditorFor(x => x.LastName)
        </div>
        ...
    </div>
    <div class="modal-footer">
        <a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>
        <button class="btn btn-primary">Save changes</button>
    </div>
</div>

Obviously you will also need to wrap the input fields into an Html.BeginForm that will allow you to send the updated details of the employee to the server. It might also be necessary to AJAXify this form if you want to stay on the same page.

like image 74
Darin Dimitrov Avatar answered Sep 19 '22 13:09

Darin Dimitrov