将ext文本框值传递给javascript,然后传递给控制器​​操作

时间:2013-04-17 05:55:35

标签: javascript model-view-controller controller

我有一个场景,我必须将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>

函数CallLogin(){

        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/';          

                   }

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

谢谢!

1 个答案:

答案 0 :(得分:1)

你必须使用ajax和jquery来实现这一点。

1.在页眉中包含jquery库

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

2.在CallLogin()functoin

中执行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));
        }