AJAX的Javascript文本框后文的ActionResult asp.net的MVC [英] Javascript ajax post textbox text to ActionResult asp.net mvc

查看:91
本文介绍了AJAX的Javascript文本框后文的ActionResult asp.net的MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML

 <输入类型=密码ID =LoginPasswordText称号=密码的风格=宽度:150像素/>


<输入类型=按钮ID =LoginButton1值=保存级=LoginButton1Class的onclick =LoginButton1OnClick/>
 

的Json

  VAR TextBoxData = {
文字:LoginPasswordText.GetValue()
};


   功能LoginButton1OnClick(){
        $阿贾克斯({
            网址:/首页/ MyActionResult
            键入:POST,
            数据类型:JSON,
            的contentType:应用/ JSON的,
            数据:JSON.stringify(TextBoxData)

            成功:函数(MYDATA){
               警报(成功);
            }
        });
        返回true;
    }
 

MyActionResult

 公众的ActionResult MyActionResult(字符串文本)
{
 返回视图();
}
 

以上codeS(HTML,JSON,MyActionResult)的作品非常好,但它是JSON数据。

我要发送超过codeS为阿贾克斯data.And我尝试以下code。 但是低于code没有工作,如果我点击按钮,我不能发送任何数据,并有nothing.Where我错过?

 <脚本>
    功能LoginButton1OnClick(){

         VAR TextBoxData = {
    文字:LoginPasswordText.GetValue()
    };


        $阿贾克斯({
            键入:POST,
            网址:首页/ MyActionResult
            数据:TextBoxData,
            成功:函数(){
                警报(成功);
            }
        });

    }
< / SCRIPT>
 

解决方案

您不需要额外的对象来包装。你也有无效的JavaScript。还有的 LoginPasswordText.GetValue()调用产生一个JavaScript错误后尾随逗号。另外为了找回您可以使用 .VAL输入字段的值() 功能。

所以,简单地发送值-是:

 <脚本>
    功能LoginButton1OnClick(){
        VAR文字= $('#LoginPasswordText)VAL()。

        $阿贾克斯({
            键入:POST,
            网址:首页/ MyActionResult
            数据:文本,
            成功:函数(){
                警报(成功);
            }
        });
    }
< / SCRIPT>
 

这将发送字符串值以下的控制器操作:

  [HttpPost]
公众的ActionResult MyActionResult(文本字符串)
{
    返回查看();
}
 

Html

<input type="password" id="LoginPasswordText" title="Password" style="width: 150px" />


<input type="button" id="LoginButton1" value="Save" class="LoginButton1Class" onclick="LoginButton1OnClick" />

Json

var TextBoxData = {
Text: LoginPasswordText.GetValue(),
};


   function LoginButton1OnClick() {
        $.ajax({
            url: "/Home/MyActionResult",
            type: "POST",
            dataType: "json",
            contentType: 'application/json',
            data: JSON.stringify(TextBoxData),

            success: function (mydata) {
               alert("Success");
            }
        });
        return true;
    }

MyActionResult

public ActionResult MyActionResult(string Text)
{
 return view();
}

Above codes(Html,Json,MyActionResult) works very well but it is json data.

I want to send above codes as ajax data.And i tried below code. However below code does not work , if i click to button , i can not send any data and there is nothing.Where i miss ?

<script>
    function LoginButton1OnClick() {

         var TextBoxData = {
    Text: LoginPasswordText.GetValue(),
    };


        $.ajax({
            type: "POST",
            url: "Home/MyActionResult",
            data: TextBoxData,
            success: function () {
                alert('success');
            }
        });

    }
</script>

解决方案

You don't need to wrap in an additional object. Also you have invalid javascript. There's a trailing comma after the LoginPasswordText.GetValue() call resulting in a javascript error. Also in order to retrieve the value of the input field you could use the .val() function.

So simply send the value as-is:

<script>
    function LoginButton1OnClick() {
        var text = $('#LoginPasswordText').val();

        $.ajax({
            type: "POST",
            url: "Home/MyActionResult",
            data: text,
            success: function () {
                alert('success');
            }
        });
    }
</script>

This will send the string value to the following controller action:

[HttpPost]
public ActionResult MyActionResult(string text)
{
    return View();
}

这篇关于AJAX的Javascript文本框后文的ActionResult asp.net的MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆