[英]Using javascript to call controller method in MVC
Im trying to make a table row work as a link to another view in my mvc website. 我试图让一个表行作为我的mvc网站中另一个视图的链接。 Instead of using the standard "Details" link provided by the auto generated table list, I would like to use the table row as a link to the "Details" view instead.
我不想使用自动生成的表列表提供的标准“详细信息”链接,而是使用表格行作为“详细信息”视图的链接。 So somehow I need to make the row work as a link.
所以我需要以某种方式将行作为链接。 Each rom has a unique id that I need to pass on to the controller method.
每个rom都有一个唯一的id,我需要传递给控制器方法。 I have tried different solutions but noting happens when I press on the table row...
我尝试了不同的解决方案但是当我按下表格行时发生了注意......
So far this is what I have: 到目前为止,这就是我所拥有的:
<script type="text/javascript">
$(document).ready(function(){
$('#customers tr').click(function () {
var id = $(this).attr('id');
$.ajax({
url: "Customer/Details" + id,
succes: function () { }
});
})
})
</script>
My controller method: 我的控制器方法:
public ActionResult Details(int id)
{
Customer model = new Customer();
model = this.dbEntities.Customers.Where(c => c.Customer_ID == id).Single();
return View(model);
}
Global.asax: Global.asax中:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"CustomerDetails",
"Customer/Details/{id}",
new { controller = "Customer", action = "Details", id = "" }
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// Use LocalDB for Entity Framework by default
Database.DefaultConnectionFactory = new SqlConnectionFactory(@"Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
Here is what I would do: 这是我要做的:
<tr data-id='@SomeRazorDataId' class="MyAction">foo</tr>
And then: 接着:
$(document).ready(function(){
$('.MyAction').live("click",function () {
var id = $(this).attr('data-id');
window.location = "Customer/Details/" + id;
})
});
If you are using jQuery 1.7+, you should use the on()
method rather than the live()
method. 如果您使用的是jQuery 1.7+,则应使用
on()
方法而不是live()
方法。
Good luck! 祝好运!
There is a typo in your code; 您的代码中存在拼写错误 ;
success:
//----^
A couple of things: 有几件事:
/
) between the action and the parameter: url: "Customer/Details/" + id
, otherwise, you'll invoke an Action
called Details123
, for example, which doesn't exist; /
): url: "Customer/Details/" + id
,否则,你将调用一个名为Details123
的Action
,它不存在; id
, ie, Customer/Details/{id}
: id
,即Customer/Details/{id}
: success
, not succes
. success
,而不是succes
。 Your Global.asax
should have something along these lines: 你的
Global.asax
应该有以下几点:
routes.MapRoute(
"CustomerDetails",
"Customer/Details/{id}",
new { controller = "Customer", action = "Details", id = ""}
);
I had this type of situation lately and opted to use the Ajax helper class: 我最近有这种情况,并选择使用Ajax助手类:
@Ajax.ActionLink("Details", "Details",
new
{
id = Model.Id
}, null)
In this example it would assume that you want a link saying 'details' and you're already in the Customer controller. 在这个例子中,它假设你想要一个说“详细信息”的链接,而你已经在Customer控制器中。
Either way, look at the helper classes if all you want to do is fire a controller action from a link, gives you a bit more strong typing in terms of how to pass/handle the id value etc 无论哪种方式,如果您想要做的就是从链接触发控制器操作,请查看帮助程序类,在如何传递/处理id值等方面为您提供更强的输入
The url that you have tried to call is invalid: 您尝试调用的网址无效:
"Customer/Details" + id, “客户/详细信息”+ id,
instead it should be "Customer/Details&id=" + id 相反它应该是“Customer / Details&id =”+ id
(OR) (要么)
use 'data' 使用'数据'
$.ajax({ url: "Customer/Details", data:{id:id}, succes: function () { } }); $ .ajax({url:“Customer / Details”,data:{id:id},succes:function(){}});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.