[英]Is there any way to call JavaScript in an MVC4 ActionLink for one of the RouteValue parameters?
I have a drop down list (DropDownListFor) and an ActionLink on my page. 我的页面上有一个下拉列表(DropDownListFor)和一个ActionLink。 Basically, the problem I'm having is I'm trying to capture the selected value from my drop down list and passing that into my ActionLink as an ID. 基本上,我遇到的问题是我正在尝试从下拉列表中捕获选定的值并将其作为ID传递到我的ActionLink中。 Here's my code: 这是我的代码:
@Html.DropDownListFor(x => x.Capsules, new SelectList(Model.Capsules, "pk", "name", "pk"))
<br />
@Html.ActionLink("Submit", "Create",
new { controller = "Process", id = /*JavaScript here to get the selected ID for the DropDownList above*/ },
new { data_role = "button" })
For what I'm trying to accomplish, is there a way to embed JavaScript into my Html.ActionLink call? 对于我想要实现的目标,有没有办法将JavaScript嵌入到我的Html.ActionLink调用中? If there's not a way, or if it's not recommended, could you please advise of another solution to solve this problem? 如果没有办法,或者不推荐,请告知另一种解决方案来解决这个问题? Thanks in advance! 提前致谢!
You can put dummy value for the id
parameter like this : 您可以为id
参数添加虚拟值,如下所示:
@Html.ActionLink("Submit", "Create",
new { controller = "Process", id = "dummy" },
new { data_role = "button" })
Then replace that value when the link is clicked. 然后在单击链接时替换该值。
// Assuming your link's id is `submit`, and the dropdown's id is `capsules`
$('#submit').click(function() {
var id = $('capsules').val();
$(this).href = $(this).href.replace('dummy', id);
});
You can do this via intercepting the link using javascript Darin has posted an example of this. 你可以通过使用javascript拦截链接来实现这一点Darin已经发布了一个这样的例子 。
However, it looks like you're trying to submit some values using an ActionLink, and you're probably better off creating a viewmodel which holds all the values you want, and then posting everything using a submit button. 但是,看起来您正在尝试使用ActionLink提交一些值,并且您可能最好创建一个包含所需值的viewmodel,然后使用提交按钮发布所有值。 This allows you to post more data than just the ID, prevents you from being dependent on Javascript, and keeps all of the code server side instead of mixing and matching. 这允许您发布比ID更多的数据,防止您依赖Javascript,并保持所有代码服务器端而不是混合和匹配。
Judging by the small code you've posted - you already have a model, probably some strongly typed entity, and it has a property called Capsules. 根据您发布的小代码判断 - 您已经有了一个模型,可能是一些强类型实体,并且它有一个名为Capsules的属性。
In your controller, create the view model which holds the view's data: 在控制器中,创建保存视图数据的视图模型:
public class YourViewModel
{
YourModel YourModel { get; set; }
public int CapsuleId { get; set; }
}
Then your view: 然后你的观点:
@using( @Html.BeginForm( "Create", "Process" ) )
{
@Html.DropDownListFor(m=> m.CapsuleId, new SelectList(Model.YourModel.Capsules, "pk", "name", "pk"))
<input type="submit">
}
Then your controller action to handle this: 然后你的控制器动作来处理这个:
[HttpPost]
public ActionResult Create( YourViewModel model )
{
var id = model.CapsuleId;
// do what you're going to do with the id
return View();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.