使用ASP.NET MVC,我想在JQuery的Button Click上调用控制器的动作。
Controller:
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult PersonInfo(string name, string city)
{
// other code for assign ViewData
return View();
}
}
我在Index.chtml页面中有两个textBox和一个Button。我想显示类似“ http://localhost:2526/PersonName”的网址。对于城市,我想要可选的性能指标,并且不想在网址中使用。因此,我将路线映射如下:
routes.MapRoute(
"Default", // Route name
"",
new { controller = "Home", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"PersonInfo", // Route name
"{name}", // URL with parameters
new { controller = "Home", action = "PersonInfo", name= "" , city = ""} // Parameter defaults
从浏览器中,如果我输入'http://localhot:2526/John'这样的网址,则PersonInfo视图成功显示。
我访问了link与我的问题有关。但我也想传递参数。
任何人都可以帮助我,如何在JQuery中单击按钮时调用操作。
例如,您可以将参数作为查询字符串传递:
$('#buttonId').click(function(){
document.location =
'@Url.Action("MyAction","MyController")'+'?'+'parameterToSend = @value';
});
您的链接会像
MyController/MyAction?parameterToSend=value
您想在Url.Action方法内添加参数以避免在主机末尾使用/。
@@ rl.Action(“ PersonInfo”,“ Home”,新的{name = Model.Name,city = Model.City})///我相信这会查看您的路线并生成localhost:2526 / John / JohnsCity
您的参数是模型的一部分吗?如果是这样,上述方法将起作用。如果不是,您可以使用1AmirJalali答案,但请在附加参数之前替换/,因为如果参数不属于您的模型,则它们可能来自javascript变量。