简体   繁体   中英

JavaScript Failing in Partial View MVC

I had a MVC 5 view which has a tab control. I have the following view

@using System.Collections
@using MyComp.Content.Text;
@using MyComp.Utilities;
@using System.Linq;
@model MyComp.Models.ViewModels.AdministratorViewModel

<style type="text/css">
    ...
</style>

<script src="~/Scripts/jquery-ui.js"></script>
<script src="~/Scripts/jquery-2.0.0.js"></script>

<div class="manage">
    <ul class="nav nav-tabs" id="myTab">
        <li class="active">
            <a data-toggle="tab" href="#invite">
                <span class="glyphicon glyphicon-inbox"></span>Invite
            </a>
        </li>
        <li>
            <a data-toggle="tab" href="#custom_invite">
                <span class="glyphicon glyphicon-pencil"></span>Custom Invite
            </a>
        </li>
    </ul>
    <div class="tab-content">
        <div id="invite" class="tab-pane active fade in">
            @Html.Partial("_InvitePartial", Model)
        </div>
        <div id="custom_invite" class="htmlCode tab-pane fade">
            @Html.Partial("_CustomInvitePartial", Model)
        </div>
    </div>
</div>
@section scripts {
    <script>
        function onSuccess(result) {
            $('#notify_failure_message').html(result.notify_failure);
            $('#notify_success_message').html(result.notify_success);
        }
    </script>
    <script>
        $(function () {
            $("input[type=button]").click(function () {
                var data_email = $('#email').val();
                var data_product = $('#product option:selected').text();
                $.ajax({
                    url: 'Tools/SendInvite',
                    type: 'POST',
                    data: { email: data_email, product: data_product },
                    success: function (result) {
                        $('#fail_message').html(result.result_failure);
                        $('#success_message').html(result.result_success);
                    }
                });
            });
        });
    </script>
}

and my partial view as

@using System.Collections
@using MyComp.Content.Text;
@using MyComp.Utilities;
@using System.Linq;
@model MyComp.Models.ViewModels.AdministratorViewModel

@using (Html.BeginForm("SendInvite", "Tools", FormMethod.Post,
    new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()
    <h2>Invite Users</h2>
    <div class="form-group">
        @Html.LabelFor(m => m.EmailAddress, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.EmailAddress,
                new { @class = "form-control", id = "email" })
            @Html.ValidationMessageFor(m => m.EmailAddress)
        </div>
    </div>
    <div class="form-group">
        @Html.Label("Access To", new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.DropDownList("Product", new SelectList(
                new List<Object> {
                    new { Text = "ProcuctA", Value = "ProcuctA" },
                    new { Text = "ProcuctB", Value = "ProcuctB" },
                    new { Text = "All", Value = "All" }},
                    "Value",
                    "Text"),
                    new { @class = "form-control", id = "product" })
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <span id="fail_message" class="label label-danger"></span>
            <span id="success_message" class="label label-success"></span>
            @*<p class="text-info">Test Message</p>*@
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="button" id="invite_button" value="Invite User" class="btn btn-primary" />
        </div>
    </div>
}

Bu my button id="invite_button" won't fire the controller method. When I had everything in a single view it did fire and all was fine, this was using the same java script, why has this stopped working when I have moved to a partial view?

Thanks for your time.


Edit. my main view that hosts the partial views is

@using System.Collections
@using VisasysNET.Content.Text;
@using VisasysNET.Utilities;
@using System.Linq;
@model VisasysNET.Models.ViewModels.AdministratorViewModel

<style type="text/css">
    span {
        padding-right: 10px;
    }
    ...
</style>

<script src="~/Scripts/jquery-ui.js"></script>
<script src="~/Scripts/jquery-2.0.0.js"></script>

<div class="manage">
    <ul class="nav nav-tabs" id="myTab">
        <li class="active">
            <a data-toggle="tab" href="#invite">
                <span class="glyphicon glyphicon-inbox"></span>Invite
            </a>
        </li>
        <li>
            <a data-toggle="tab" href="#custom_invite">
                <span class="glyphicon glyphicon-pencil"></span>Custom Invite
            </a>
        </li>
    </ul>
    <div class="tab-content">
        <div id="invite" class="tab-pane active fade in">
            @Html.Partial("_InvitePartial", Model)
        </div>
        <div id="custom_invite" class="htmlCode tab-pane fade">
            @Html.Partial("_CustomInvitePartial", Model)
        </div>
    </div>
</div>
@section scripts {
    <script>
        function onSuccess(result) {
            $('#notify_failure_message').html(result.notify_failure);
            $('#notify_success_message').html(result.notify_success);
        }
    </script>
    <script>
        $(function () {
            $("input[type=button]").click(function () {
                var data_email = $('#email').val();
                var data_product = $('#product option:selected').text();
                $.ajax({
                    url: 'Tools/SendInvite',
                    type: 'POST',
                    data: { email: data_email, product: data_product },
                    success: function (result) {
                        $('#fail_message').html(result.result_failure);
                        $('#success_message').html(result.result_success);
                    }
                });
            });
        });
    </script>
}

invite_button does not have any javascript referencing it. Neither is it of type submit . This button will always do nothing.

It looks like you expect it to be of type submit .

<input type="submit" id="invite_button" value="Invite User" class="btn btn-primary" />

Try modifying the input button type from submit to button - it will work.. I tried with alert in a sample application and it worked.. for me. Below is the code. Here's the MVC index view

<script type="text/javascript">   

    $(document).ready(function () {
        $("input[type=button]").click(function () {
            alert("hello from partial class");
        });
    });

</script>

<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">
        http://asp.net/mvc</a>.
</p>
<div>
    @Html.Partial("~/Views/Home/_MyTest.cshtml")

and here the partial view

@{
    ViewBag.Title = "_MyTest";
}

<div>
    <input type="button" id="invite_button" value="Invite User"/>
</div>

So on click on the button I am able to the jquery and alert popup's up

hope this helps..

you need to render scripts in your layout view.

<body>
   <!-- Other Code -->
   @RenderSection("scripts", required: false)
</body>

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