开发者

View Navigation using buttons in MVC

开发者 https://www.devze.com 2023-03-30 08:12 出处:网络
I am trying to create a View with 2 buttons that redirect users to other views but nothing happens when they are clicked. What am I doing wrong? Thanks in advance.

I am trying to create a View with 2 buttons that redirect users to other views but nothing happens when they are clicked. What am I doing wrong? Thanks in advance.

<% using (Html.BeginForm()) {%>
<p>
    Please LogIn or Register
</p>

<p>
<button name="button" type="button"onclick="document.location.href=$('#logInUrl').attr('href')">LogIn</button>
<button name="button" type="button" onclick="document.location.href=$('#createAccountUrl').attr('href')">Register</button>
<a id="logInUrl" href="<%= Html.AttributeEncode(Url.Action("Account", "LogOn")) %>" style="display:none;"></a>
<a id="createAccountUrl" href="<%= Html.AttributeEncode(Url.Action("Account", "Register")) %>" style="display:none;"></a>
</p>
<% } %>

Working Code

<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" language="javascript" type="text/javascript"></script>

<script language="JavaScript" type="text/javascript">
    $(document).ready(function() 
    {
        $('#button1').click(function () 
        {
        document.location.href = $('#logInUrl').attr('href');
        });

        $('#button2').click(function () 
        {
        document.lo开发者_运维问答cation.href = $('#createAccountUrl').attr('href');
        });
    });

</script>
<% using (Html.BeginForm()) {%>

<p>
    Please LogIn or Register
</p>

<p>
<button name="button1" type="button" id="button1">LogIn</button>
<button name="button2" type="button" id="button2">Register</button>
<a id="logInUrl" href="<%= Html.AttributeEncode(Url.Action("LogOn", "Account")) %>" style="display:none;"></a>
<a id="createAccountUrl" href="<%= Html.AttributeEncode(Url.Action("Register", "Account")) %>" style="display:none;"></a>
</p>
<% } %>

</asp:Content>


Since it looks like you are using jquery, don't use onclick on the button but jquery click function of the buttons like

 $('#button1').click(function() {
       document.location.href=$('#logInUrl').attr('href');
 });
 $('#button2').click(function() {
       document.location.href=$('#createAccountUrl').attr('href');
 });



<button name="button" type="button" id="button1">LogIn</button>
<button name="button" type="button" id="button2">Register</button>
0

精彩评论

暂无评论...
验证码 换一张
取 消