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();
}
-
can you add more code, what are you using for models, is this for unit testing? what framework are you using.atmd– atmd2015年02月03日 14:45:19 +00:00Commented 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(); }Siva Ganesh– Siva Ganesh2015年02月03日 14:47:35 +00:00Commented 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.Andrei Dvoynos– Andrei Dvoynos2015年02月03日 15:12:33 +00:00Commented Feb 3, 2015 at 15:12
3 Answers 3
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.
1 Comment
$('#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 logicFrom 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);
Comments
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.