【问题标题】:Passing ext textbox values to javascript and then to controller action将 ext 文本框值传递给 javascript,然后传递给控制器​​操作
【发布时间】:2013-04-17 05:55:34
【问题描述】:

我有一个场景,我必须将用户名和密码值从 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/';          

                   }

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

谢谢!

【问题讨论】:

    标签: javascript model-view-controller controller


    【解决方案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));
            }
    

    【讨论】:

    • 嗨懒鬼..谢谢你的回复..但是当我尝试你的代码时..按钮没有提交给控制器..因此页面没有提交..当我尝试同样在 IE 浏览器中..它会发出一个错误,说 $ 未声明..知道为什么吗?
    • 您需要在页面中包含 jquery 库才能使其正常工作。查看我修改后的答案
    • 我已将以下代码添加到我的 aspx 文件中......但是代码正在运行正如预期的那样仅在 Mozilla 中
    • 下载最新版本的jquery库并提供其路径而不是ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
    • 您已经包含了两个不同版本的 jquery。删除一个
    猜你喜欢
    • 1970-01-01
    • 2021-12-16
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多