如何将选择的 DropDownList 值传递给我的控制器函数,然后在 TextArea 中显示输出

How to pass the DropDownList value selected to my controller function to then show the output in a TextArea

我正在尝试 select 我的 DropDownList 中的文件名,然后使用 select 框的名称文件调用我的 GetFile 函数,然后将其输出到 TextArea。

但我无法将数据传递给 GetFile(nameFile)

我的 DropDownList 包含我的 xml 文件名:

<div class="card" style="padding:20px">
    <h4 class="mb-0">Sélection d'un fichier model XML </h4>
    @Html.DropDownListFor(model => model.ListeFichiers, ((Dictionary<string, string>)Model.ListeFichiers).Select(e => new SelectListItem() { Text = e.Key, Value = e.Value }).ToList(),
 new { @class = "form-control", @id = "listeFichierTransmettre" })
</div>

我的 GetFile 函数,它将从 DropDownList 检索到的文件名作为参数

public string GetFile(string nomFichier)
{ 
    string path = @"C:\xmlFiles\" + nomFichier;
    string fileContent;
    using (StreamReader streamReader = new StreamReader(@path, Encoding.UTF8))
    {
        fileContent = streamReader.ReadToEnd();
    }
    return fileContent;
}

之后我想输出在 TextArea 中返回的 GetFile 字符串:

    <div class="card-body" id="XMLResult">
        @Html.TextArea("Info", new { cols = "75", rows = "15", @readonly = "readonly", @disabled = "disabled", style = "min-width:100% !important;" })
    </div>

我试过了,但我的文件名总是空的,所以很明显我做错了什么:

$(document).ready(function () {
    $("#listeFichierTransmettre").change(function () {

        $.ajax({
            type: "POST",
            url: '/Information/GetFile',
            data: $('#listeFichierTransmettre').serialize(),              
            dataType: 'json',
            success: function (result) {
                console.log(result);
            }
        })
        return false;
    });
});

将脚本更改为:

$(document).ready(function() {
  $("#listeFichierTransmettre").change(function() {

    $.ajax({
      type: "POST",
      url: '/Information/GetFile',
      data: {
        nomFichier: $("#listeFichierTransmettre").find(":selected").val()
      },
      dataType: 'json',
      success: function(result) {
        console.log(result);
        $("#Info").val(result);
      }
    })
    return false;
  });
});