简体   繁体   English

有没有办法在MVC4 ActionLink中为其中一个RouteValue参数调用JavaScript?

[英]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.

相关问题 如何在actionlink mvc 4中调用javascript函数? - how to call a javascript function in actionlink mvc 4? 在MVC4中的控制器内调用Javascript表单 - Call a Javascript form within a controller in MVC4 根据下拉列表更新EditorFor:是否可以在MVC4的HTML帮助器中使用JavaScript变量? - Update EditorFor according to a Dropdown: Is there any way to use a JavaScript variable inside a HTML helper on MVC4? Ajax ActionLink OnSuccess调用带参数的javascript函数,错误:必需对象 - Ajax ActionLink OnSuccess call javascript function with parameters, Error: Object required 使用TempData完成时,MVC3 Ajax ActionLink可以调用javascript函数 - MVC3 Ajax ActionLink to call javascript function when completed with TempData 无法在MVC4中的Select标签更改时调用javascript函数 - Unable to call a javascript function onchange of a Select tag in MVC4 将JavaScript检测与MVC4显示模式集成的最佳方法是什么? - What is the best way to integrate JavaScript detection with MVC4 display modes? 如何使Fancybox在MVC4剃须刀中使用HTML ActionLink工作 - How to get fancybox working with html actionlink in mvc4 razor 在mvc4中使用ajax.actionlink会刷新页面 - Using ajax.actionlink in mvc4 causes to refresh the page Javascript有条件地构建mvc actionlink - Javascript to build mvc actionlink conditionally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM