简体   繁体   English

ASP.Net MVC ActionLink

[英]ASP.Net MVC ActionLink

I'm trying to create an ActionLink in one of my views that sends the selected value of a dropdown list to a new action. 我试图在我的一个视图中创建一个ActionLink,该操作将下拉列表的选定值发送到新操作。 So far I have this, I just need to find a way to populate the ID on the end of my ActionLink. 到目前为止,我已经有了,我只需要找到一种方法即可在ActionLink的末尾填充ID。

   <%= Html.DropDownList("StatusDropDown") %>
   <%= Html.ActionLink("Apply","Index",new {Controller="Tasks", Action="Index", id="DROPDOWN LIST SECLECTED VALUE"}) %>

Obviously this link would need to be updated whenever the selected index of the drop down is changed. 显然,无论何时更改下拉列表的选定索引,都需要更新此链接。 Is this something I need to do in javascript or is there a better way of managing this from within ASP.Net MVC? 这是我需要在javascript中做的事情,还是有一种更好的方法可以从ASP.Net MVC内进行管理?

Thanks 谢谢

If you don't want to use form submission (ie, want the parameter passed as part of the url instead of a form parameter), you'll need to build the url client-side with javascript. 如果您不想使用表单提交(即,希望将参数作为url的一部分而不是表单参数传递),则需要使用javascript构建url客户端。

<%= Html.DropDownList("StatusDropDown") %>
<a id="applyLink" href="#">Apply</a>

<script type="text/javascript">

    function setHref( elem, val )
    {
        if (val) {
           $(elem).attr( "href", "/Tasks/" + val );
           $("#applyLink").unbind("click");
        }
        else {
           $(elem).attr( "href", "#" );
           $("#applyLink").click( function() { alert( "No value chosen" ); } );
        }
    }

    $(function() {
       var dropdown = $("#StatusDropDown");
       dropdown.change( function() {
           setHref( this, $(this).val() );
       });
       setHref( dropdown, null );
    });
</script>

A link goes to another page, it is in effect a redirect. 链接转到另一个页面,实际上是重定向。 The only way to update where that link goes to with reference to the drop down list is with javascript. 引用下拉列表更新该链接的链接的唯一方法是使用javascript。

It sounds like you want a kind of submit action. 听起来您想要一种提交动作。 In that case you should use a form and a submit button, creating the appropriate handlers in your controller. 在这种情况下,您应该使用表单和提交按钮,在控制器中创建适当的处理程序。 Remember you can just do a redirect in your controller based upon the submitted value of the form. 请记住,您仅可以根据表单的提交值在控制器中进行重定向。 So something like this: 所以像这样:

<form method="post" action="/MyForm">
    <input type="select" name="mySelect">
        <option value="1">First Option</option>
        <option value="2">Second Option</option>
    </input>
</form>

And in your controller: 在您的控制器中:

public ActionResult MyForm(int mySelect)
{
    return Redirect(String.Format("myurl?id={0}", mySelect));
    // Note the above is only preferable if you're going to an external link
    // Otherwise you should use the below:
    return RedirectToAction("myAction", new { id = mySelect });
}

Obviously in this simplified example, the MyForm proxy to your desired action is redundant, but it illustrates the idea so you can apply it to your specific situation. 显然,在这个简化的示例中,MyForm代理对您所需的操作是多余的,但是它说明了这个想法,因此您可以将其应用于您的特定情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM