Run Modal Open Screen Script in Ajax. C#

Asked

Viewed 213 times

0

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

I am opening a modal view with the following code:

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

Open view 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>
}
  • Do not put your javascript in the modal, but in the original view (which opens the modal) and it will work.

  • So, there it is, but if I put my Javascript in the original view I can’t get the so much of the view that is in the modal!

  • what would be the "so" view?

1 answer

0

So I analyzed your code, if the modal is loaded by ajax, include the js code in the ajax Success, it will be yours .ready

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.