与值id Html.ActionLink从一个DropDownList [英] Html.ActionLink with id value from a dropdownlist

查看:455
本文介绍了与值id Html.ActionLink从一个DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DropDownList:
<%= Html.DropDownList(ddlNames,新的SelectList(Model.NameList,ID,姓名))%>

I've got a dropdownlist: <%= Html.DropDownList("ddlNames", new SelectList(Model.NameList, "ID", "Name"))%>

我有一个ActionLink的:
&LT;%:Html.ActionLink(编辑,编辑,成员,新{面积=MembersAreaID = XXX},NULL)%>

I've got an ActionLink: <%: Html.ActionLink("edit", "Edit", "Members", new { area = "MembersArea", id = XXX }, null)%>

我想在XXX DropDownList中的价值。
所以我想对在ActionLink的一个视图中使用的控制值。
这有可能以简单的方式?

I want the value of the dropdownlist in the XXX. So I want to use values from controls on a view in the ActionLink. Is that possible in a simple manner?

感谢,

菲利普

推荐答案

您不能这样做,因为HTML辅助执行服务器端同时下拉值可以在客户端改变。实现这一目标的唯一方法是使用JavaScript。你可以注册下拉的onchange事件和修改锚的href的值:

You can't do this because the html helpers execute at the server side while the dropdown value can change at the client side. The only way to achieve it is to use javascript. You could register for the onchange event of the dropdown and modify the value of the href of the anchor:

$(function() {
    $('#ddlNames').change(function() {
        var value = this.value; // get the selected value
        // TODO: modify the value of the anchor
    });
});

这是可能不是最好的解决方案,因为路由在服务器端,并以修改你需要做的客户端上的一些字符串处理链接的值来配置。

This is probably not the best solution because the routes are configured on the server side and in order to modify the value of the link you need to do some string manipulation on the client side.

作为一种替代方法,你可以使用一个表单和一个提交按钮,而不是一个锚。这种方式的下拉选定值将被自动发送到服务器,你不需要任何JavaScript:

As an alternative you could use a form and a submit button instead of an anchor. This way the selected value of the dropdown will be automatically sent to the server and you don't need any javascript:

<% using (Html.BeginForm("Edit", "Members", new { area = "MembersArea" })) { %>
    <%= Html.DropDownListFor(x => x.SelectedName, 
        new SelectList(Model.NameList, "ID", "Name"))%>
    <input type="submit" value="Edit" />
<% } %>

这篇关于与值id Html.ActionLink从一个DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆