【问题标题】:Issue while store the javascript textbox value into DB using ajax code in MVC2在 MVC2 中使用 ajax 代码将 javascript 文本框值存储到 DB 时出现问题
【发布时间】:2011-07-22 08:24:35
【问题描述】:

我可能在这里遗漏了一些明显的东西,但是我怎么能重写这段代码!

我在这里尝试存储在文本框中输入的值(文本框显示在 javascript 对话框页面中)。在 javascript 对话框页面中,我有一个“确定”按钮..当我单击按钮时,我想存储值在文本框中输入。我想使用 Ajax 保存内容。

请看我的示例代码 查看页面:

<script language="javascript" type="text/javascript">
    $(function () {
        $('.button').live('click', function () {
            $('.Text_dialog').dialog('open');
        });

        $('.Text_dialog').dialog({
            autoOpen: false,
            buttons: {
                'Ok': function () {
                    var textValue = $(':txtValue').val();
                    $.ajax({
                        url: '/Home/About',
                        type: 'POST',
                        data: { str: textValue },
                        success: function (result) {
                            alert(result.val);
                        }
                    });
                },
            }
        });
    });

</script>


     <input type="button" value="Add Value" class="button" />
     <div class="Text_dialog" title="Basic modal dialog">
          TextValue: <input type="text" class="txtValue" />
</div>

控制页面:

[HttpPost]
    public ActionResult About(string str)
    {
        ValidateClass ObjAM = new ValidateClass();
        int value = ObjAM.ValidatetextValue(str);
        return Json(new { val = value });
    }

模型页面:

 public class ValidateClass
    {
        DataClasses1DataContext dbObj = new DataClasses1DataContext();
        public int ValidatetextValue(string str)
        {
            string value = (from SearchtextValue in dbObj.Options
                                where SearchtextValue.OptionName == str
                                select SearchtextValue.OptionName).Single();
            if (value == null)
            {
                return 1;
            }
            return 0;

        }
    }

当我运行此代码时,我收到诸如“对象不支持此属性或方法”之类的脚本错误。请建议

【问题讨论】:

    标签: javascript ajax asp.net-mvc-2


    【解决方案1】:

    data: { str: textValue } 更改为 data: "{ 'str' :'" + textValue +"' }"

    如果您对正确转义引号感到困惑,试试这个。

    $(function() {
        $('.button').live('click', function() {
            $('.Text_dialog').dialog('open');
        });
    
        $('.Text_dialog').dialog({
            autoOpen: false,
            buttons: {
                'Ok': function() {
                    var DTO = "{ 'str' :'" + $('.txtValue').val() +"' }";
                    $.ajax({
                        url: '/Home/About',
                        type: 'POST',
                        data: DTO,
                        success: function(result) {
                            alert(result.val);
                        }
                    });
                },
            }
        });
    });
    

    请注意,我已将 :txtValue 更改为 .txtValue,因为它是正确的选择器。

    【讨论】:

    • 当页面自行加载时遇到问题,例如“类型”未定义。请帮助
    • 小心引号。并确保有完整的逗号
    • 代码工作正常..但空值存在于我的控制页面而不是我在文本框中输入的字符串..
    • @Dhana: 你用.txtValue 代替冒号
    • yes.. 请在这里找到我的代码 var txtValue = "{ 'str' :'" + $('#txt1').val() + "' }"; $.ajax({ url: '/Home/About', type: 'POST', dataType: "json", data: txtValue, success: function (result) { alert(result.val); } });跨度>
    【解决方案2】:

    你可以尝试改变吗

    <input type="text" class="txtValue" />
    

    <input type="text" class="txtValue" id="txtValue" />
    

    var textValue = $(':txtValue').val();
    

    var textValue = $('#txtValue').val();
    

    我从未使用过: 选择器...但我认为它会返回一个集合,您应该使用索引[0],或者,为什么不使用类选择器.

    编辑:我see 在这种情况下你不能使用:

    您也可以在您的 Ajax 请求中添加 dataType: "json",并从

    更改您的方法返回类型
    public ActionResult About(string str)
    

    public JsonResult About(string str)
    

    编辑 2:

    您是否包含了 dialog() 函数所在的库?在包含 jquery 库之后?

    【讨论】:

    • 是的,我在 JQ lib 之后添加了 Lib 文件
    • 代码工作正常..但空值存在于我的控制页面而不是我在文本框中输入的字符串..请建议
    • 你应该让它保持原样......data: { str: textValue },这是正确的方式,在这种情况下,你正在发送一个对象,该对象将被请求反序列化并以相同的名称序列化,在服务器端...
    猜你喜欢
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2019-07-29
    相关资源
    最近更新 更多