根据表单中单击的按钮调用操作

时间:2013-06-27 13:45:00

标签: asp.net-mvc

我有一个登录/注册表单,每个功能都有一个按钮

<div id="ManualLogin">        
    @using (Html.BeginForm(new { Controller = "Companies", Action = "ManualLogOn" }))
    {            
        <fieldset>
            <legend>Enter details</legend>
            <div class="editor-label">
                @Html.LabelFor(u => u.Name)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(u => u.Name)                
            </div>
            <div class="editor-label">
                @Html.LabelFor(u => u.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(u => u.Password)                
            </div>

            <input type="submit" id="LogInButton" name="Command" value="Log in"/>
            <input type="submit" id="RegisterButton" name="Command" value="Register"/>

            <label id="ServerValidaiotnLabel">@ViewBag.Error</label>

        </fieldset>            
    }
</div>

如何根据点击的按钮更改此设置以使其调用操作。 (登录/注册)

1 个答案:

答案 0 :(得分:1)

您可以在按钮上切换表单操作,然后单击并手动提交表单。 这里有样本:

表单标记的更改:

<buttom id="LogInButton" data-action="@Url.Action("login", "user")" class="Command">Log in</button>
<button id="RegisterButton" data-action="@Url.Action("register", "user")" class="Command"></button>

jQuery:

<script type="text/javascript">

        (function($){

        $('form button.Command').on('click', function() {
              var url = $(this).data('action');
              $(this).closest('form').attr('action', url).submit();
        });

        })(jQuery)

</script>