0

我有一个场景,我必须将用户名和密码值从 ext 文本字段传递到控制器操作以对数据库进行验证。我写了以下代码

   <ext:TextField Name="Username" runat="server" ID="UserName"></ext:TextField>
   <ext:TextField Name="Password" runat="server" ID="Password"></ext:TextField>
   <ext:Button runat="server" ID="submit1" Type="Submit"  Text="LOGIN" >
    <Listeners>
        <Click Fn="CallLogin">

        </Click>
    </Listeners>

    </ext:Button>

函数调用登录(){

        var username = document.getElementById('UserName').value;
        var password = document.getElementById('Password').value;

        //var url = '@Url.Action("Login", "Index")';
        //window.location.href = url;

        window.location.href = '/Login/Index/';          

                   }

如何将这些值传递给控制器​​操作?

谢谢!

4

1 回答 1

1

您将不得不使用 ajax 和 jquery 来实现这一点。

1.在页面标题中包含 jquery 库

<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> 
</head>

2.在CallLogin()函数中执行ajax调用

function CallLogin() {
        var username = document.getElementById('UserName').value;
        var password = document.getElementById('Password').value;

  $.ajax({
                    type: "POST",
                    url: '/Login/Index/',
                    data: { userText: username , passwordText: password },
                    success: function (response) {
                        if (response > 0) {
                           alert("Success");
                          }
                        else
                         {
                        alert("Login failed");
                           }

    });

}

假设您的操作方法看起来像这样:

  [AcceptVerbs(HttpVerbs.Post)]
        public int Index(string userText, string passwordText)
        {

           return (checkValidUser(userText,passwordText));
        }
于 2013-04-17T06:06:35.300 回答