C# 根据DropDownList的选定值更改URL id
我有一个下拉列表,上面有一个汽车模型 @Cars,选择一个值,htmlAttributes:new{@class=form control} 我需要用户必须选择一个值,如果他选择了一个特定的值,也必须将该值的id作为参数发送到操作的urlC# 根据DropDownList的选定值更改URL id,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个下拉列表,上面有一个汽车模型 @Cars,选择一个值,htmlAttributes:new{@class=form control} 我需要用户必须选择一个值,如果他选择了一个特定的值,也必须将该值的id作为参数发送到操作的url 您需要使用javascript来实现这一点。例如,你可以给你的锚一个唯一的id: <a id="editRegistration" href="@Url.Action("Edit", "Registration", new { id = "initia
您需要使用javascript来实现这一点。例如,你可以给你的锚一个唯一的id:
<a id="editRegistration" href="@Url.Action("Edit", "Registration", new { id = "initial value from the database" })" class="text-white">Modify Registration</a>
然后订阅下拉列表的更改事件并更新锚定的href:
<script>
$('#Cars').change(function() {
var url = "@Url.Action("Edit", "Registration", new { id = "#ID#" })";
// replace the #ID# placeholder with the selected value
var newHref = url.replace('#ID#', this.value);
$('#editRegistration').attr('href', newHref);
// do the same with the other anchor
});
</script>
嗨,Darin,我不知道用户会从下拉列表中选择哪个值来评估IDY,你不需要评估任何东西。这只是一个占位符,你应该像我的答案中所示的那样硬编码。我们的想法是,您使用javascript订阅下拉列表的onchange事件,在这个回调中,您只需用实际选定的值替换占位符,并用新的url替换锚点的href属性。我不理解这个词://用选定的值替换ID占位符。这意味着什么?哪个选择的值?这个.value,如我的答案所示。在onchange回调中,this.value将包含下拉列表的选定值。因此,您只需将其插入url:var newHref=url.replace'ID',this.value;。