简体   繁体   中英

Load my partial view in a specific div at the same page after a click

I have this code

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult ManageEmployee(int cntID, string command)
{
    repository = new ContactRepository();
    if (!String.IsNullOrEmpty(command) && command.Equals("edit"))
    {
        LKIContact employees = repository.GetById(cntID);

        ViewBag.IsUpdate = true;
        return PartialView("_ManageEmployee", employees);
    }
    else
    {
        ViewBag.IsUpdate = false;
        return PartialView("_ManageEmployee");
    }
}

When I click on :

<a href='Home/ManageEmployee?cntID=${cntID}&command=edit' class='editDialog'>Edit</a>

I execute the first part of the above method to display my populated form for update when I click on href='Home/ManageEmployee?cntID=0&command=create' class='openDialog' I open the empty form to add a new employee.

My problem now is that when I click on edit or create I have the desired result but in another page. What I want is to slide out those forms in the specific divs (below) while staying at the same page.(under the grid)

<div id="dialog-edit"></div>
<div id="dialog-create" style="display: none"></div>

here is the code where my grid will be loaded and the location of the divs where I want my forms to be loaded

@section featured {
<section class="featured">
    <div class="content-wrapper">
        <!-- the igHierarchicalGrid target element-->
        <table id="employeeGrid" style="width: auto;"></table>
        <p>
            <!--<a id='openDialog' href='Home/CreateEmployee?cntID=0' class='openDialog ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only' style="color: white;"><button>Add Employee</button></a>-->
       </p>
    </div>
</section>

<br />
<div id="dialog-confirm" style="display: none">
    <p>
        <span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
            Are you sure to delete ?
    </p>
</div>

<div id="dialog-edit"></div>

<div id="dialog-view" style="display: none"></div>

}

Thank you for your help

You can use ajax call to get partial view and render it in your div:

<script type="text/javascript">
  $(document).on("click", "#btnEdit", function () {

        $.ajax({
            url: "Home/ManageEmployee?cntID=" + myEmployeeID + "&command=edit";
            cache: false,
            success: function (html) {
                $("#dialog-edit").append(html);
            }
        });

    });
</script>

Add this script at the bottom of your page, replace btnEdit with the id of your edit button and replace the MyEmployeeID with the employeeid you want to edit.

If you have an edit button for each row, you would include the EmployeeId within the link tag, perhaps as a data attribute so

<a href='Home/ManageEmployee?cntID=${cntID}&command=edit' data-employeeid='${cntID}' class='editor'><button>Edit</botton></a>

Then using the example from afzalulh just pick up on the data attribute

<script type="text/javascript">
  $(document).on("click", ".editor", function () {

        var myEmployeeID = $(this).data('employeeid');

        $.ajax({
            url: "Home/ManageEmployee?cntID=" + myEmployeeID + "&command=edit";
            cache: false,
            success: function (html) {
                $("#dialog-edit").append(html);
            }
        });

    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM