javascript - Catching a textbox value which is stored in a variable -
this controller.
public class homecontroller : controller { public static string commstring = ""; public homecontroller() { fwutility.connstring = "data source=.;initial catalog=northwind;uid=sa;password=123"; } public actionresult index() { return view(); } public static datatable getdata(string customerid) { string strfilter = "'" + customerid + "%'"; commstring = "select * customers customerid " + strfilter; return fwutility.getdatatable(commstring); }
this view.
<script type="text/javascript"> function buttonclick() { var value = $('#text1').val(); alert('@homecontroller.getdata("d").rows.count.tostring()'); } </script> <input type="text" id="text1" value="a" /> <input type="text" id="text2" /> <input type="button" value="show" id="button1" onclick="buttonclick()" />
i want catch value stored in textbox , display in alert box, have mentioned "d". in controller have sql query assigned.and here calling controller. out put suppose put "a" inside text box , click button, count of names starting letter "a". this
it's not clear question, assuming want more simple alert(value)
, then:
i suggest add additional action go getdata
function return row count directly:
public actionresult getdatarowcount(string id) { var count = getdata(id).rows.count; return new jsonresult { data = new { count }, jsonrequestbehavior = jsonrequestbehavior.allowget }; }
then can use jquery ajax value controller:
<script type="text/javascript"> function buttonclick() { var value = $('#text1').val(); $.get({ url: '@url.action("getdatarowcount", "home")', data: { id = value }, success: function(result) { alert(result.count); } }); } </script> <input type="text" id="text1" value="a" /> <input type="button" value="show" id="button1" onclick="buttonclick()" />
Comments
Post a Comment