简体   繁体   English

将值从下拉列表传递给html actionlink mvc4 razor

[英]pass value from dropdown to html actionlink mvc4 razor

I am trying to pass a value from a drop down list to an existing link, however nothing seems to work: 我试图将值从下拉列表传递到现有链接,但似乎没有任何工作:

index.cshtml: index.cshtml:

@Html.ActionLink("Download", "MyDownload", null, new { @class = "button" })  
@Html.DropDownList("schools", "-- All Schools --")  

in my SchoolsController: 在我的SchoolsController中:

public FileContentResult MyDownload(string school="")
{
    List1<>
    List2<>

    var vals = 
         from list1
         join list2
         from
         select new {
             };

    if(school ! ="")
    {
        vals.where(x.list2.schools==school).tolist(); **// I get this error: "Object reference not set to an instance of an object."**
    }    
}

Something I can do easily in other languages, I find learning C# MVC takes forever. 我可以在其他语言中轻松完成,我发现学习C#MVC需要永远。

You could use jQuery to monitor the change Event on your dropdownlist. 您可以使用jQuery监视下拉列表中的更改事件。 Now, I'm not entirely sure if you're trying to change the text that is used for the link or the actual href of the link. 现在,我不完全确定您是否尝试更改用于链接的文本或链接的实际href。 Below I've created some code to change both. 下面我创建了一些代码来改变它们。

View 视图

$(document).ready(function () {
    $("#schools").change(function () {
        var selection = $("#schools").val();
        var link = $("#Download");

        //Change text
        link.text(selection);

        //Change href
        link.attr("href", "linkHref" + selection)
    });
});

It will look something like this, pending what you're wanting to do with your link and dropdownlist's selection. 它看起来像这样,等待你想要处理你的链接和下拉列表的选择。

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

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