简体   繁体   English

在控制器函数MVC中获取Html.DropDownList值

[英]Get Html.DropDownList Value in a Controller Function MVC

I need to get the selected value of a dropdownlist when I click an ActionLink. 单击ActionLink时,需要获取下拉列表的选定值。

请参阅ActionLink和DropDownLists

Here is the dropdownlist I am binding from controller. 这是我从控制器绑定的下拉列表。

 @Html.DropDownList("resource", new SelectList(ViewBag.ResourceList, ViewBag.SelectedResource), "Resource", new { @class = "span6", @style = "width:14%; color:black;"})

And ActionLink with the function without [HttpPost] 和带有[Link]的功能的ActionLink

@Html.ActionLink("Export Data", "ExportData");

I have tried from Request.Form["resource"] but it gives me null all the time 我已经尝试过Request.Form["resource"]但它一直使我为null

public ActionResult ExportData()
        {
            var req = Request.Form["resource"];
        }

I just need to get the text value whatever is in the DropDownList inside my ExportData Function. 我只需要获取文本值,就可以在ExportData函数内的DropDownList中获得任何值。

The action link basically renders an a tag, and the a tag will look something roughly like this; 动作链接基本上呈现了a标签,该标签看起来大致类似于此;

<a href="ExportData">Export Data</a>

Because links issue a GET request, any parameters need to be passed via: 由于链接会发出GET请求,因此任何参数都需要通过以下方式传递:

<a href="ExportData?resource=xyz">Export Data</a>

Request.Form will always be empty in a get request because a POST request populates the form collection. 由于POST请求会填充表单集合,因此Request.Form始终为空。 But either way, with a GET or POST request, you can pass the data as a parameter of the action method like: 但是无论哪种方式,通过GET或POST请求,您都可以将数据作为action方法的参数进行传递,例如:

public ActionResult ExportData(string resource)

So either put a <form> around the data you want to post to the server and change the hyperlink to a button to initiate the post, or use javascript to append "?resource=VAL" to the end of the hyperlink HREF attribute, where VAL is the value from the dropdown. 因此,要么在要发布到服务器的数据周围放置一个<form> ,然后将超链接更改为按钮以启动发布,或者使用javascript将“?resource = VAL”附加到超链接HREF属性的末尾,其中VAL是下拉菜单中的值。

EDIT : in the few scenarios I had to do this before, what I'll normally do is on the link, add a data attribute (in C# API, use data_ for data attributes): 编辑 :在之前我不得不做的几种情况下,我通常要做的是在链接上添加数据属性(在C#API中,对数据属性使用data_):

@Html.ActionLink("Export Data", "ExportData", new { data_url = "ExportData" })

The reason why I use a data attribute is to preserve the original URL and that way I don't have to do string manipulation. 我使用数据属性的原因是保留原始URL,这样我就不必进行字符串操作。 Using jQuery, on resource value change, you can update the URL easily: 使用jQuery,在资源值更改时,您可以轻松地更新URL:

$("#resource").on("change", function(e) {
  var val = $(this).val();
  var href = $("#linkid").data("url") + "?resource=" + val;
  $("#linkid").attr("href", href);
});

Anytime the dropdown changes, it updates the link url. 每当下拉列表更改时,它都会更新链接网址。

You should try the GetValues() method: 您应该尝试GetValues()方法:

public ActionResult ExportData()
{
    var req = Request.Form.GetValues("resource");
}

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

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