admin管理员组文章数量:1304545
In MVC3. I have a button class"open-deleteRowDialog" when i click on it goes to this javascript:
$(document).on("click", ".open-DeleteRowDialog", function () {
var pwd= '@Url.("GeneratePsw","Admin")';
$(".modal-body #pwd").val(pwd);
$('#1').modal('show');
})
what i want is to call a method (in controller), who has to return a string, that´s what i want to save in "var pwd" to show in a model (view)
the method (in controller) is:
public string GeneratePsw()
{
HomeBridgeEntities ddbb = new HomeBridgeEntities();
SqlConnection Cn = new SqlConnection(((System.Data.EntityClient.EntityConnection)ddbb.Connection).StoreConnection.ConnectionString);
SupPassGenerator sup = new SupPassGenerator(Cn);
//psw conteins a password from CreateRandomPassword
string psw = sup.CreateRandomPassword(9);
return psw;
}
thank you!
In MVC3. I have a button class"open-deleteRowDialog" when i click on it goes to this javascript:
$(document).on("click", ".open-DeleteRowDialog", function () {
var pwd= '@Url.("GeneratePsw","Admin")';
$(".modal-body #pwd").val(pwd);
$('#1').modal('show');
})
what i want is to call a method (in controller), who has to return a string, that´s what i want to save in "var pwd" to show in a model (view)
the method (in controller) is:
public string GeneratePsw()
{
HomeBridgeEntities ddbb = new HomeBridgeEntities();
SqlConnection Cn = new SqlConnection(((System.Data.EntityClient.EntityConnection)ddbb.Connection).StoreConnection.ConnectionString);
SupPassGenerator sup = new SupPassGenerator(Cn);
//psw conteins a password from CreateRandomPassword
string psw = sup.CreateRandomPassword(9);
return psw;
}
thank you!
Share Improve this question asked Mar 14, 2013 at 15:13 AlsanAlsan 3251 gold badge5 silver badges12 bronze badges 2- You need to use AJAX. – SLaks Commented Mar 14, 2013 at 15:14
- could you please write any example in AJAX?? thank you! – Alsan Commented Mar 14, 2013 at 15:16
2 Answers
Reset to default 6Make an ajax call to your controller action method. You may use $.get
method like below.
$(function(){
$(document).on("click", ".open-DeleteRowDialog", function () {
var pwd="";
$.get("@Url.Action("Yourcontroller","GeneratePsw")",function(data){
pwd=data;
//now do whatever you want with pwd variable;
});
})
});
$.get is a short form of $.ajax
method with type HTTP GET.
If you have trouble like cached data in the response, you may add a unique timestamp in your get
call so that you won't get the cached result. You may use $.now method.
$.get("@Url.Action("Yourcontroller","GeneratePsw")?"+$.now(),function(data){
// to do : do something with result
});
Another way is to set the cache property value to false in ajaxSetup method. But this will be applicable to all ajax calls.
Use jQuery ajax and call the controller method directly as a url
$(document).on("click", ".open-DeleteRowDialog", function () {
var pwd="";
$.get('Yourcontroller/GeneratePsw', function(data){
pwd=data;
//now do whatever you want with pwd variable;
});
})
本文标签: aspnet mvc 3MVC3 Calling controller method from JavascriptStack Overflow
版权声明:本文标题:asp.net mvc 3 - MVC3 Calling controller method from Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741788782a2397538.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论