MVC:从Javascript运行Controller方法 [英] MVC: Run Controller method from Javascript

查看:152
本文介绍了MVC:从Javascript运行Controller方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎很容易,但不仅仅适合我。我有以下代码从我的javascript文件在我的HomeController中运行一个方法

This seems easy but is not just working for me. I have the following code to run a method in my HomeController from my javascript file

function(id)
 alert("here");
 $.ajax({
   url: '/HomeController/MethodName,
   data: { id: id }
   success: function(data){
    alert('Sucess ' + data);      
  }
 });



我的方法是


My method is

public string MethodName(int id)
{
   return id.ToString() + "test ";
}







调用javascript的视图定义了以下脚本:jquery.unobtrusive -ajax.min.js,MicrosoftMvcAjax.js,MicrosoftAjax.js,validate.unobtrusive.min.js,jquery.validate.min.js



但是不是刚刚工作。什么都没发生。该功能的第一个警报确实显示出来。但之后别无其他。




The view calling the javascript has the following scripts defined: jquery.unobtrusive-ajax.min.js, MicrosoftMvcAjax.js, MicrosoftAjax.js, validate.unobtrusive.min.js, jquery.validate.min.js

But is is not just working. Nothing happens. The first alert of the function do show up. But nothing else after that.

推荐答案

.ajax({
url:' / HomeController / MethodName,
data:{id:id}
success:function(data){
alert('
Sucess ' + data);
}
});
.ajax({ url: '/HomeController/MethodName, data: { id: id } success: function(data){ alert('Sucess ' + data); } });



我的方法是


My method is

public string MethodName(int id)
{
   return id.ToString() + "test ";
}







调用javascript的视图定义了以下脚本:jquery.unobtrusive -ajax.min.js,MicrosoftMvcAjax.js,MicrosoftAjax.js,validate.unobtrusive.min.js,jquery.validate.min.js



但是不是刚刚工作。什么都没发生。该功能的第一个警报确实显示出来。但之后别无其他。




The view calling the javascript has the following scripts defined: jquery.unobtrusive-ajax.min.js, MicrosoftMvcAjax.js, MicrosoftAjax.js, validate.unobtrusive.min.js, jquery.validate.min.js

But is is not just working. Nothing happens. The first alert of the function do show up. But nothing else after that.


关闭,但不完全是如何工作的。以下是它应该如何显示的示例。

Close, but not quite how it works. Here is a sample of how it should look.


.ajax({
type:' GET'
url:' / YourController / YourMethod'
dataType:' json'
async false
数据: {arg1:' arg1value'
arg2:' arg2value'
},
成功:函数(结果){alert(result);},
错误:function(jqXHR,textStatus,errorThrown){
alert(' 加载时出错:' + textStatus + + errorThrown + + jqXHR.responseText);
}
});
.ajax({ type: 'GET', url: '/YourController/YourMethod', dataType: 'json', async: false, data: { arg1: 'arg1value', arg2: 'arg2value' }, success: function (result) { alert(result); }, error: function (jqXHR, textStatus, errorThrown) { alert( 'An error occured while loading: ' + textStatus + ": " + errorThrown + ": " + jqXHR.responseText ); } });




[HttpGet]
public JsonResult YourMethod(string arg1, string arg2)
{
   // Do something to get a result...
   string yourResult = "Some soft of result.";
   return Json(yourResult, JsonRequestBehavior.AllowGet);
}


这篇关于MVC:从Javascript运行Controller方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆