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?

廉价感情. 提交于 2019-12-11 02:35:38

问题


I have created an auto-populated drop downlist on my MVC 4 web application. When the user selects an item from the dropdown list, I want the partial view to show the item selected rather than all items as it already displays in this partial view. How do I pass the string value parameter of the selected list item from an auto-populated dropdown list to a partial view controller? Here is the code for my auto populated dropdown list:

 @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>
      }
  }

I want to pass the m.TargetName string as a parameter so that i can manuipulate the partialview based on which list item is selected. The partialview consists of progress bars for jobs being performed where the data is stored on a SQL Server DB. I use the following Ajax Script to refresh the original partial view every 3 seconds. I need to be able to do the same with the selectedd partial views. Again, this is all autopopulated, so I am assuming the best way is to go by the 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  

回答1:


Both the Action and RenderAction methods have overloads that allow you to pass additional parameters that are appended to the query string for the resource.

If you are doing this on the server side, you can:

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

If you are doing this on the client side, you would have to write some additional jQuery to make it work (or just plain old 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);
    }});
}

I used "TargetName" because I didn't know what your parameter name is.



来源:https://stackoverflow.com/questions/28701611/how-do-i-pass-the-string-value-parameter-of-the-selected-list-item-from-an-auto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!