1

これは些細な質問かもしれません。私は新しい蜂です:)これが私のViewコードです

<button type="button" id="btnLogin" onclick="location.href='<%: Url.Action("Authenticate", "Login") %>'" runat="server"></button>

これが私のコントローラーコードです

 public ActionResult Authenticate(string username,string password)
    {
        bool status = Login.Authenticate(username, password);
        return View("Status", status);
    }

そのコントローラーに行くことはできますが、パラメーターを渡すことができません。コントローラー関数にパラメーターを渡すための儀式的な方法を教えてください。

フル ビュー コード

<table>
    <tr>
        <td>
            <input type="text" id="txtUsername" runat="server" />
        </td>
        <td>
            <input type="text" id="txtPassword" runat="server" />
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center">
            <button type="button" id="btnLogin" onclick="location.href='<%: Url.Action("Authenticate", "Login") %>'" runat="server"></button>
        </td>
    </tr>
</table>
4

5 に答える 5

3

属性を使用して入力値を渡すことができnameます。

<%using (Html.BeginForm("Authenticate", "Login"))
{%>
    <table>
        <tr>
            <td>
                <input type="text" id="txtUsername" name="username" />
            </td>
            <td>
                <input type="text" id="txtPassword" name="password"  />
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="submit" value="Login" class="submit_button"/>
            </td>
        </tr>
    </table>
<%}%>

コントローラ

[HttpPost]
public ActionResult Authenticate(string username, string password)
{
    bool status = Login.Authenticate(username, password);
    return View("Status", status);
}
于 2012-12-11T14:52:52.853 に答える
3

パラメータを追加できるurlアクションへの3番目のパラメータがあることを知っています

@Url.Action("Authenticate", "Login" , new {name = "name"})

そこに自分の価値を追加する方法が必要なだけです

于 2016-04-05T14:04:59.223 に答える
0

Javascriptを使用して呼び出し文字列にパラメータを設定する必要があります

<script type="text/javascript">
    function postdata(location) {
        var url = '<%: Url.Action("Authenticate", "Login", new { username = XXX, password = YYY) %>';
        var Username = document.getElementById(Username).value;
        var Password= document.getElementById(Password).value;
        url = url.replace("XXX", Username );
        url = url.replace("YYY", Password);
        location.href=url;
    }
</script>

<button type="button" id="btnLogin" onclick="postdata()" runat="server"></button>
于 2012-12-11T15:16:32.297 に答える
0

<form>コントローラーの Authenticate メソッドに投稿する a を使用します。また、 を に変更<button>します<input type="submit"

このような:

<form method="post" action="@Href("~/account/authenticate")" id="LogOnForm">
.... @*input boxes*@
<input type="submit" value="Login"/>
</form>

編集: ajax ログインの場合は、このスクリプトを使用します (jquery が必要です)。フォームにはID

<script type="text/javascript">
      $(document).ready(function () {
            $("#LogOnForm").submit(function (event) {
                event.preventDefault();                
                $.ajax({
                    type: "POST",
                    url: '@Url.Content("~/account/authenticate")',
                    data: $("#LogOnForm").serialize(),
                    success: function (data) {                       
                       if (data.Status) {
                            //Logged in succesfully
                         } else {
                            //Login failed
                         }
                    },
                    error: function (xhr, ajaxOptions, thrownError) {                            
                        console.log(xhr.responseText);
                    }
                });
            });
      });

    </script>

編集: コントローラーの変更:

public JsonResult Authenticate(string username,string password)
    {
        bool status = Login.Authenticate(username, password);
        return Json( new @"Status", status);
    }
于 2012-12-11T14:47:20.717 に答える
0

入力フィールドに name プロパティがありません。ユーザー名とパスワードの入力ボックスに name プロパティを追加します。name プロパティは、アクション コントローラーの引数と同じである必要があります。

<table>
    <tr>
        <td>
            <input type="text" id="txtUsername" runat="server" name="username" />
        </td>
        <td>
            <input type="text" id="txtPassword" runat="server"  name="password"/>
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center">
            <button type="button" id="btnLogin" onclick="location.href='<%: Url.Action("Authenticate", "Login") %>'" runat="server"></button>
        </td>
    </tr>
</table>
于 2015-10-28T06:23:43.303 に答える