Ad

Bind A Button Event From Partial View In Other View Mvc

- 1 answer

I have MVC Project I have a button in the partial and i want to bind on click event of that button from other view . the partial loads, i can see the modal. however the click event on the button present in the partial view never gets invoked.

My partial View :

          <br />
        <div align="center">
            <input type="button" ID="BtnLogin" value="تسجيل الدخول" Class="GreenButtonStyle" />
        </div>

My View :

<head>
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body >
<div style="width:100%;height:100%;">
    <br />
    <div style="text-align: center; align-items: flex-start;">
        <h3>تسجيل الدخول  </h3>
    </div>
    <br />
    <div align="center" vertical-align="middle">
        @Html.Partial("_LoginUC")
    </div>

</div>

My Controller :

    public ActionResult Login(AdminLoginClass user, string ReturnUrl = "")
    {
        string message = "";


                        string username = user.AdminUserName;
                        string Password = user.AdminPassWord;


                        {
                        DBModel.DBAdminLogin DB_Model = new DBModel.DBAdminLogin();
                        AdminLoginClass AdminLoginClass = DB_Model.Login(username, Password);

                            int ID = AdminLoginClass.AdminGroupID;
                            switch (ID)
                            {

                                default:
                                    #region
                                    {
                                        return RedirectToAction("Index", "Home");

                                    }
                                    #endregion
                            }

                            ViewBag.Message = message;
                            return View(user);
                        }

        ViewBag.Message = message;
        return View();
    }

here is my JavaScript code i write it inside the main view :

<script>
$(document).ready(function () {
    $(".BtnLogin").on('click', function (event) {
        debugger;
        var requestedDataType = $(this).attr('data-value');
        $.ajax(
            {
                type: "GET",
                data: { requestedDataType: requestedDataType },
                url: "Login/Login",
                success: function (data) {
                    debugger;
                    $('#BtnLogin').html(data);
                },
                error: function () {
                    alert('Error')
                }
            }
        );
    }
    );
});

Ad

Answer

i was forgeten to add

 @using (@Html.BeginForm(null, "Login", FormMethod.Post))
{
    @Html.AntiForgeryToken();
 \\\\\\\ }

it works after adding this 2 lines

Ad
source: stackoverflow.com
Ad