0

I have to capture text box value and display in div which is generated from model.

For example: I need to display "textbox1" value or "textbox2" based on logic. So that I append text from ajax call.

Below one is the c# statement

return "Expected value is \"+$('#txtAMC').val();

when I am append this value to div as $('#div1').html(result); it shown as below

Expected value is "+$('#txtAMC').val()

But i want result as

Expected value is textbox value

Can any body help how can i pass text to ajax call. Thanks in advance.

Javascript

$.ajax({ 
 url: '/XMLCorrections/XMLCorrections/GetUserInput', 
 type: 'POST', 
 dataType: 'json', 
 contentType: "application/json; charset=utf-8", 
 success: function (result) { 
 $('#div1').html(result)
}); 

c#

public string GetUserInput() {
 // some Logic return "Expected value is \"+$('#txtAMC').val(); 
}
codeandcloud
55.5k47 gold badges169 silver badges259 bronze badges
asked Feb 3, 2015 at 14:43
3
  • can you add more code, what are you using for models, is this for unit testing? what framework are you using. Commented Feb 3, 2015 at 14:45
  • i am using c#4.5, mvc 4. jquery Code: $.ajax({ url: '/XMLCorrections/XMLCorrections/GetUserInput', type: 'POST', dataType: 'json', contentType: "application/json; charset=utf-8", success: function (result) { $('#div1').html(result)}); c# Code: public string GetUserInput() {// some Logic return "Expected value is \"+$('#txtAMC').val(); } Commented Feb 3, 2015 at 14:47
  • Don't return javascript back from your controllers. Instead return a serialized json object and do whatever you need to do with it on the success function of the jquery post. Commented Feb 3, 2015 at 15:12

3 Answers 3

1

You can set the "data" attribute in the ajax post. you can do something like -

var txtBoxText = $('#txtAMC').val()
var data = {"passedValue":txtBoxText}
$.ajax({
 type: "POST",
 url: url,
 data: data,
 success: success,
 dataType: dataType
});

Then do whatever you are doing with this value at server and return the value.

Please clarify if I didn't get the question right.

answered Feb 3, 2015 at 15:07
Sign up to request clarification or add additional context in comments.

1 Comment

you are right @stripathi but in my case i am generating $('#txtAMC').val() at model only. that code is xmlCode= Regex.Replace(currentNode.ToString(), "<AMC>(.)*</AMC>", "<AMC>$('#txtAMC').val()</AMC>", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); ; htmlCode= "It is an Boeing Account, but model info is not available. please enter AMC value in below text box or click on cancel for Manual Entry.<br/><br/><b>AMC : </b><input type='text' id='txtAMC' value='" + missingData[0].AMC + "'></input>"; this is actual code what i am implementing in logic
0

From what I understand, you are getting this as result on callback

result = "Expected value is $('#txtAMC').val()";

and what you want is

result = "Expected value is blah";

where $('#txtAMC').val() === "blah"

The best option you have is to change the return of GetUserInput to the id of the textbox you require like this

return "txtAMC";

Now on the success callback you can change it to suit your need like this

var resultValue = $("#" + result).val();
$('#div1').html("Expected value is " + resultValue);
answered Feb 3, 2015 at 15:18

Comments

0

I try to split the function and create an anonymous function then I am iterated that function. Through this logic I achieved output what exactly I want.

var splitedData=data.split('+');
var formateText= function(val){ 
 try{
 var fun=new Function('return '+ val); return fun()
 } 
 catch(e){
 return val
 }
}
 var resultedData="";for(var i=0;i<splitedData.length;i++){
 resultedData+=formateText(splitedData[i]);
 }

If there is any simple solution then please provide it.

answered Feb 3, 2015 at 15:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.