如何将所选列表项的字符串值参数从自动填充的下拉列表传递到 MVC 4 中的分部视图控制器?

How do I pass the string value parameter of the selected list item from an auto-populated dropdown list to a partial view controller in MVC 4?

我在我的 MVC 4 Web 应用程序上创建了一个自动填充的下拉列表。当用户从下拉列表中选择一个项目时,我希望局部视图显示所选项目而不是所有项目,因为它已经显示在该局部视图中。 如何将所选列表项的字符串值参数从自动填充的下拉列表传递到分部视图控制器? 这是我自动填充的下拉列表的代码:

 @foreach (var m in ViewDatamodel)
 {
      if (m.State == "In Work")
      {
           <li><a href="@Html.RenderAction("_GetforStatus", "Status" /@* I am assuming that I need to place the TargetName here, but not too sure exactly how *@)">@m.TargetName</a></li>
      }
  }

我想将 m.TargetName 字符串作为参数传递,以便我可以根据选择的列表项操作部分视图。局部视图由正在执行的作业的进度条组成,其中数据存储在 SQL 服务器数据库中。我使用以下 Ajax 脚本每 3 秒刷新一次原始局部视图。我需要能够对选定的部分视图执行相同的操作。同样,这都是自动填充的,所以我假设最好的方法是通过 TargetName:

 <script>
      function loadpartialView()  {
           $.ajax({
                url:  '@Url.Action("_GetfoeStatus', "Status")',
                type:   'POST',
                data:  '{}',  //I am assuming that I will need to pass the parameter to here
                cache:  'false',
                async:  'true',
                success:  function (result)  {
                     $('#progress').html(result);
                }
            });
       }
       $(function() //..... Refresh Timer  

A​​ction 和 RenderAction 方法都有重载,允许您传递附加到资源查询字符串的附加参数。

如果您在服务器端执行此操作,您可以:

Html.RenderAction("_GetForStatus", "Status", new { TargetName = m.TargetName })

如果您在客户端执行此操作,则必须编写一些额外的 jQuery 才能使其正常工作(或者只是普通的旧 Javascript)。

function loadpartialView()  {
  $.ajax({
    url:  '@Url.Action("_GetfoeStatus', "Status")',
    type:   'POST',
    data:  { TargetName: $("#YourDropDown").val() }
    cache:  'false',
    async:  'true',
    success:  function (result)  {
      $('#progress').html(result);
    }});
}

我用了"TargetName"因为我不知道你的参数名是什么