Run Modal Open Script in Ajax. W#

0

Problem: I'm in the view index, it has a button that opens the Create view on a modal screen, on that modal screen I want to execute a javascript code, but for some reason it is not running.

I'm opening a modal view with the following code:

$("#btnNovo").click(function () {
  $(".modal-body").load("/Pais/Create", function () {
    $("#exampleModal").modal("show");
  });
});

View open in modal.

@model Projeto.WebERP.EntityFramework.Entities.Pais
@{
    ViewBag.Title = "Create";
    Layout = null;
}

<h2> Cadastro de País </h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.Handle)

    <hr />
    <div class="form-horizontal">
        <div class="row">
            <!-- DESCRICAO -->
            <div class="col-md-4">
                <label for="txtDescricao"> Descrição: </label>
                @Html.TextBoxFor(model => model.Descricao, new { type = "text", @class = "form-control", id = "txtDescricao" })
                @Html.ValidationMessageFor(model => model.Descricao, "", new { @class = "text-danger" })
            </div>
            <!-- SIGLA -->
            <div class="col-md-2">
                <label for="txtSigla"> Sigla: </label>
                @Html.TextBoxFor(model => model.Sigla, new { type = "text", @class = "form-control", id = "txtSigla" })
                @Html.ValidationMessageFor(model => model.Sigla, "", new { @class = "text-danger" })
            </div>
        </div>
        <br />
        <div class="row">
            <!-- DATACADASTRO -->
            <div class="col-md-4">
                <label for="txtDataCadastro"> Data Cadastro: </label>
                @Html.TextBoxFor(model => model.DataCadastro, new { type = "datetime", @class = "form-control", readOnly = true, id = "txtDataCadastro" })
                @Html.ValidationMessageFor(model => model.DataCadastro, "", new { @class = "text-danger" })
            </div>

            <!-- DATAALTERACAo -->
            <div class="col-md-4">
                <label for="txtDataAlteracao"> Data Alteração: </label>
                @Html.TextBoxFor(model => model.DataAlteracao, new { type = "datetime", @class = "form-control", readOnly = true, id = "txtDataAlteracao" })
                @Html.ValidationMessageFor(model => model.DataAlteracao, "", new { @class = "text-danger" })
            </div>
        </div>
        <br />
        <div class="row">
            <div class="col-md-2">
                <input type="submit" id="sumitForm" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}
@section Scripts{
    <script>
    $(document).ready(function () {
        alert("dsdsd");
    });
    </script>
}
    
asked by anonymous 23.08.2017 / 20:04

1 answer

0

So I've analyzed your code, if the modal is loaded by ajax, include the js code in ajax's success, it will be your .ready

$("#btnNovo").click(function () {
  $(".modal-body").load("/Pais/Create", function () {
    $("#exampleModal").modal("show");
    alert("alerta");
  });
});
    
24.08.2017 / 19:07