vendredi 24 juillet 2015

Avoid postback on OnClick event of View

I have two views as below :

Index.cshtml

    <p>
       @Html.ActionLink("Create New", "Create", "Owner ", new { id = "lnkCreate" })
    </p>

 @section JavaScript {
        <script type="text/javascript" src="@Url.Content("/Scripts/Owner.js")"></script>

    }

Create.cshtml

 <div>
            @Html.ActionLink("Back", "Index", "Owner", new { id = "lnkBack" })
        </div>

@section JavaScript {
    <script type="text/javascript" src="@Url.Content("/Scripts/Owner.js")"></script>

}

Script >> Owner.js

$(document).ready(function () {


   $('#lnkBack').click(function (e) {                  // not working
        alert('hello')
        e.preventDefault();
        $.ajax({
            url: '/Owner/Index',
            dataType: "html",
            success: function (data) {
                //$('#divContent').hide();
                //$('#divGrid').html(data);
            }
        });

    });

    $('#lnkCreate').click(function (e) {                 //working fine

        e.preventDefault();
        $.ajax({
            url: '/Owner/Create',
            dataType: "html",
            success: function (data) {
               // $('#divGrid').hide();
               // $('#divContent').html(data);
            }
        });

    });

});

What I want to accomplish that whenever i click on "Create New" link it is working as coded without any postback. But when i click on "Back" link of the different view under same controller it is doing postback and not going to click event as in written in script code.

Aucun commentaire:

Enregistrer un commentaire