0

I may be missing something obvious here, but how could I rewrite this code!

i am trying here to store the value entered in the textbox(Textbox was showed in javascript dialog page).In a javascript dialog page i have one 'ok' button.. when i click the button i want to store the value entered in the textbox.I want to save the content using Ajax.

Please see my sample code View page:

<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>

Control page:

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

Model page:

 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;

        }
    }

When i run this code i am getting script error like "Object doesn't support this property or method".Please advice

2 Answers 2

0

Change data: { str: textValue } to data: "{ 'str' :'" + textValue +"' }"

If you are confused on escaping the quotes properly, try this.

$(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);
                    }
                });
            },
        }
    });
});

Please note that I have changed the :txtValue to .txtValue as it is the correct selector.

Sign up to request clarification or add additional context in comments.

5 Comments

I am getting issue when page loading it self like 'type' is undefined.. Please help
be careful with the quotes. and make sure there are commas intact
Code is working fine.. but null value is exists to my control page instead of string which i enter in the textbox..
@Dhana: are you using .txtValue instead of colon
yes.. Please find my code here var txtValue = "{ 'str' :'" + $('#txt1').val() + "' }"; $.ajax({ url: '/Home/About', type: 'POST', dataType: "json", data: txtValue, success: function (result) { alert(result.val); } });
0

can you try changing

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

to

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

and

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

to

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

I've never used : selector... but I think it will return you a collection and you should use index [0] or, why don't you use class selector .?

Edit: as I see you cannot use : in this case

also you can add dataType: "json",into your Ajax request, and change your method return type from

public ActionResult About(string str)

to

public JsonResult About(string str)

EDIT 2:

have you included the library where dialog() function is? after including the jquery library?

3 Comments

Yes i have added the Lib file after the JQ lib
Code is working fine.. but null value is exists to my control page instead of string which i enter in the textbox..Please advice
you should leave it as it was... data: { str: textValue } ,this is the right way, in this waz you are sending an object that will be deserialized with request and serialized with the same name, at the server side...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.