如何在选定值更改时通过Ajax调用请求调用下拉列表?

huangapple go评论46阅读模式
英文:

How to call drop down list by ajax call request when selected value changed?

问题

I work on web app mvc .NET framework I face issue i can't call function GetSelectedDropDownChanged when drop down selected changed .

我在.NET框架下的Web应用程序中工作,我遇到一个问题,当下拉列表选择改变时,我无法调用GetSelectedDropDownChanged函数。

I put break point to function GetSelectedDropDownChanged on controller WorkforceRequests

我在WorkforceRequests控制器中的GetSelectedDropDownChanged函数上设置了断点。

but it not hit or catched so what is issue

但它没有被触发,所以问题是什么?

so what I try is :

所以我尝试的是:

<th>
    <select class="form-control" id="statusselect" name="statusselectName">
        <option value="1">Pending Request</option>
        <option value="2">All requests </option>
    </select>
</th>
<th>
    <select class="form-control" id="statusselect" name="statusselectName">
        <option value="1">待处理请求</option>
        <option value="2">所有请求</option>
    </select>
</th>

when select from drop down I expect to call it ajax request call to function GetSelectedDropDownChanged controller WorkforceRequests :

当从下拉列表中选择时,我期望调用GetSelectedDropDownChanged函数,控制器是WorkforceRequests

<script>
  $(function () {
        $(document)
            .on('change', '#statusselect', function () {
                var valueofDropDown = $(this).val();
                var url = '/WorkforceRequests/GetSelectedDropDownChanged';
                var dataToSend = { selectedValue: valueofDropDown }

                $.ajax({
                    url: url,
                    data: dataToSend,
                    type: 'GET',
                    success: function (dataReceived) {
                        //update control on View
                        var receivedValue = dataReceived.myResult;
                        $('#YourControlIDToUpdate').val(receivedValue);
                    }
                })
            });
    };
</script>
<script>
  $(function () {
        $(document)
            .on('change', '#statusselect', function () {
                var valueofDropDown = $(this).val();
                var url = '/WorkforceRequests/GetSelectedDropDownChanged';
                var dataToSend = { selectedValue: valueofDropDown }

                $.ajax({
                    url: url,
                    data: dataToSend,
                    type: 'GET',
                    success: function (dataReceived) {
                        //update control on View
                        var receivedValue = dataReceived.myResult;
                        $('#YourControlIDToUpdate').val(receivedValue);
                    }
                })
            });
    };
</script>

function i call from ajax request as below :

我从下面的Ajax请求中调用函数:

public class WorkforceRequestsController : Controller
{   
    [HttpGet]
    public JsonResult GetSelectedDropDownChanged(string selectedValue) 
    {
        List<WorkforceRequest> workforceRequest = new List<WorkforceRequest>();
        if (selectedValue == "1")
        {
            workforceRequest = Workforce.GetPendingOnPalnningSection();
        }
        else
            workforceRequest = Workforce.GetAllRequestsData();

        return Json(new { myResult = workforceRequest }, JsonRequestBehavior.AllowGet);
    }
}
public class WorkforceRequestsController : Controller
{   
    [HttpGet]
    public JsonResult GetSelectedDropDownChanged(string selectedValue) 
    {
        List<WorkforceRequest> workforceRequest = new List<WorkforceRequest>();
        if (selectedValue == "1")
        {
            workforceRequest = Workforce.GetPendingOnPalnningSection();
        }
        else
            workforceRequest = Workforce.GetAllRequestsData();

        return Json(new { myResult = workforceRequest }, JsonRequestBehavior.AllowGet);
    }
}

data will returned as list from ajax call from function GetSelectedDropDownChanged ON table id dtbl

数据将作为列表从函数GetSelectedDropDownChanged的Ajax调用返回,表格ID为dtbl

<table id="dtbl" class="table table-bordered table-hover table-striped">
    <thead>
        <tr>
            <th></th>
            <th>
                @Html.DisplayNameFor(model => model.WorkforceRequestID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.DepartmentCode)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Section)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.RequiredPosition)
            </th>
        </tr>
    </thead>

    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.WorkforceRequestID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.DepartmentCode)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Section)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.RequiredPosition)
                </td>
            </tr>
        }
    </tbody>

</table>
<table id="dtbl" class="table table-bordered table-hover table-striped">
    <thead>
        <tr>
            <th></th>
            <th>
                @Html.DisplayNameFor(model => model.WorkforceRequestID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.DepartmentCode)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Section)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.RequiredPosition)
            </th>
        </tr>
    </thead>

    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.WorkforceRequestID)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.DepartmentCode)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Section)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.RequiredPosition)
                </td>
            </tr>
        }
    </tbody>

</table>

Last Updated

I change my code inside view.cshtml to :

我将代码更改为view.cshtml内部:

@model IEnumerable<HR.WorkforceRequisition.Models.WorkforceRequest>

@{
    ViewBag.Title = "Pending Requests";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Pending Requests</h2>
<hr />
@if (!string.IsNullOrWhiteSpace(ViewBag.msg))
{
    <div class="alert alert-success">
        @ViewBag.msg
    </div>
}
@if (!string.IsNullOrWhiteSpace(ViewBag.errorMsg))
{
    <div class="alert alert-danger">
        @ViewBag.errorMsg
    </div>
}
@if (Session[BLL.Common.SessionKeys.RoleCode].ToString() == "REC" || Session[BLL.Common.SessionKeys.RoleCode].ToString()

<details>
<summary>英文:</summary>

I work on web app mvc .NET framework I face issue i can&#39;t call function `GetSelectedDropDownChanged` when drop down selected changed .

I put break point to function `GetSelectedDropDownChanged` on controller `WorkforceRequests`

but it not hit or catched so what is issue 
 
so what I try is :

   

      &lt;th&gt;
        &lt;select class=&quot;form-control&quot; id=&quot;statusselect&quot; name=&quot;statusselectName&quot;&gt;
           
            &lt;option value=&quot;1&quot;&gt;Pending Request&lt;/option&gt;
            &lt;option value=&quot;2&quot;&gt;All requests &lt;/option&gt;
        &lt;/select&gt;
    &lt;/th&gt;
when select from drop down I expect to call it ajax request call to function `WorkforceRequests` controller  `WorkforceRequests` :

    &lt;script&gt;
      $(function () {
            $(document)
                .on(&#39;change&#39;, &#39;#statusselect&#39;, function () {
                    var valueofDropDown = $(this).val();
                    var url = &#39;/WorkforceRequests/GetSelectedDropDownChanged&#39;;
                    var dataToSend = { selectedValue: valueofDropDown }
    
                    $.ajax({
                        url: url,
                        data: dataToSend,
                        type: &#39;GET&#39;,
                        success: function (dataReceived) {
                            //update control on View
                            var receivedValue = dataReceived.myResult;
                            $(&#39;YourControlIDToUpdate&#39;).val(receivedValue);
                        }
                    })
                });
        };
    &lt;/script&gt; 

function i call from ajax request as below :

    public class WorkforceRequestsController : Controller
        {   
    [HttpGet]
            public JsonResult GetSelectedDropDownChanged(string selectedValue) 
            {
                List&lt;WorkforceRequest&gt; workforceRequest = new List&lt;WorkforceRequest&gt;();
                if (selectedValue == &quot;1&quot;)
                {
                  
                                          
                workforceRequest = Workforce.GetPendingOnPalnningSection();
                           
                            
                }
                else
                     workforceRequest = Workforce.GetAllRequestsData();
    
                return Json(new { myResult = workforceRequest }, JsonRequestBehavior.AllowGet);
            }
    }

data will returned as list from ajax call from function `GetSelectedDropDownChanged` ON table id `dtbl`

    &lt;table id=&quot;dtbl&quot; class=&quot;table table-bordered table-hover table-striped&quot;&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                &lt;th&gt;&lt;/th&gt;
                &lt;th&gt;
                    @Html.DisplayNameFor(model =&gt; model.WorkforceRequestID)
                &lt;/th&gt;
                &lt;th&gt;
                    @Html.DisplayNameFor(model =&gt; model.DepartmentCode)
                &lt;/th&gt;
                &lt;th&gt;
                    @Html.DisplayNameFor(model =&gt; model.Section)
                &lt;/th&gt;
                &lt;th&gt;
                    @Html.DisplayNameFor(model =&gt; model.RequiredPosition)
                &lt;/th&gt;
               
            &lt;/tr&gt;
        &lt;/thead&gt;
    
        &lt;tbody&gt;
            @foreach (var item in Model)
            {
                &lt;tr&gt;
                   
                    &lt;td&gt;
                        @Html.DisplayFor(modelItem =&gt; item.WorkforceRequestID)
                    &lt;/td&gt;
                    &lt;td&gt;
                        @Html.DisplayFor(modelItem =&gt; item.DepartmentCode)
                    &lt;/td&gt;
                    &lt;td&gt;
                        @Html.DisplayFor(modelItem =&gt; item.Section)
                    &lt;/td&gt;
                    &lt;td&gt;
                        @Html.DisplayFor(modelItem =&gt; item.RequiredPosition)
                    &lt;/td&gt;
                    
                &lt;/tr&gt;
            }
        &lt;/tbody&gt;
    
    &lt;/table&gt;

**Last Updated** 

I change my code inside view.cshtml to :

   

     @model IEnumerable&lt;HR.WorkforceRequisition.Models.WorkforceRequest&gt;
    
    @{
        ViewBag.Title = &quot;Pending Requests&quot;;
        Layout = &quot;~/Views/Shared/_Layout.cshtml&quot;;
    }
    
    &lt;h2&gt;Pending Requests&lt;/h2&gt;
    &lt;hr /&gt;
    @if (!string.IsNullOrWhiteSpace(ViewBag.msg))
    {
        &lt;div class=&quot;alert alert-success&quot;&gt;
            @ViewBag.msg
        &lt;/div&gt;
    }
    @if (!string.IsNullOrWhiteSpace(ViewBag.errorMsg))
    {
        &lt;div class=&quot;alert alert-danger&quot;&gt;
            @ViewBag.errorMsg
        &lt;/div&gt;
    }
    @if (Session[BLL.Common.SessionKeys.RoleCode].ToString() == &quot;REC&quot; || Session[BLL.Common.SessionKeys.RoleCode].ToString() == &quot;PLNG&quot;)
    {
        &lt;th&gt;
            &lt;select class=&quot;form-control&quot; id=&quot;statusselect&quot; &gt;
                @*&lt;option&gt;Select Status&lt;/option&gt;*@
                &lt;option value=&quot;1&quot;&gt;Pending Request&lt;/option&gt;
                &lt;option value=&quot;2&quot;&gt;All requests &lt;/option&gt;
            &lt;/select&gt;
        &lt;/th&gt;
    }
    &lt;table id=&quot;dtbl&quot; class=&quot;table table-bordered table-hover table-striped&quot;&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                &lt;th&gt;&lt;/th&gt;
                &lt;th&gt;
                    @Html.DisplayNameFor(model =&gt; model.WorkforceRequestID)
                &lt;/th&gt;
                &lt;th&gt;
                    @Html.DisplayNameFor(model =&gt; model.DepartmentCode)
                &lt;/th&gt;
                &lt;th&gt;
                    @Html.DisplayNameFor(model =&gt; model.Section)
                &lt;/th&gt;
                &lt;th&gt;
                    @Html.DisplayNameFor(model =&gt; model.RequiredPosition)
                &lt;/th&gt;
                &lt;th&gt;
                    @Html.DisplayNameFor(model =&gt; model.JobTitle)
                &lt;/th&gt;
                &lt;th&gt;
                    @Html.DisplayNameFor(model =&gt; model.ReportingTo)
                &lt;/th&gt;
                &lt;th&gt;
                    @Html.DisplayNameFor(model =&gt; model.NationalityCategory)
                &lt;/th&gt;
                &lt;th&gt;
                    @Html.DisplayNameFor(model =&gt; model.StatusRemark)
                &lt;/th&gt;
                &lt;th&gt;
                    Requestor
                &lt;/th&gt;
            &lt;/tr&gt;
        &lt;/thead&gt;
    
        &lt;tbody&gt;
            @foreach (var item in Model)
            {
                &lt;tr&gt;
                    &lt;td&gt;
                        @Html.ActionLink(&quot;Details&quot;, &quot;Details&quot;, new { id = item.WorkforceRequestID })
                    &lt;/td&gt;
                    &lt;td&gt;
                        @Html.DisplayFor(modelItem =&gt; item.WorkforceRequestID)
                    &lt;/td&gt;
                    &lt;td&gt;
                        @Html.DisplayFor(modelItem =&gt; item.DepartmentCode)
                    &lt;/td&gt;
                    &lt;td&gt;
                        @Html.DisplayFor(modelItem =&gt; item.Section)
                    &lt;/td&gt;
                    &lt;td&gt;
                        @Html.DisplayFor(modelItem =&gt; item.RequiredPosition)
                    &lt;/td&gt;
                    &lt;td&gt;
                        @Html.DisplayFor(modelItem =&gt; item.JobTitle)
                    &lt;/td&gt;
                    &lt;td&gt;
                        @Html.DisplayFor(modelItem =&gt; item.ReportingTo)
                    &lt;/td&gt;
                    &lt;td&gt;
                        @Html.DisplayFor(modelItem =&gt; item.NationalityCategory)
                    &lt;/td&gt;
                    &lt;td&gt;
                        @Html.DisplayFor(modelItem =&gt; item.StatusRemark)
                    &lt;/td&gt;
                    &lt;td&gt;
                        @Html.DisplayFor(modelItem =&gt; item.CreatedByName)
                    &lt;/td&gt;
                &lt;/tr&gt;
            }
        &lt;/tbody&gt;
    
    &lt;/table&gt;
    
    
    &lt;script src=&quot;http://code.jquery.com/jquery-1.9.1.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js&quot;&gt;&lt;/script&gt;
    
       
    @section scripts
        {
        &lt;script&gt;
            $(document).ready(function () {
                $(&quot;#statusselect&quot;).change(function () {
                    var valueofDropDown = $(&quot;#statusselect&quot;).val();
                    console.log(valueofDropDown);
                    var url = @Url.Action(&quot;GetSelectedDropDownChanged&quot;, &quot;WorkforceRequests&quot;);
                    var dataToSend = { selectedValue: valueofDropDown }
    
                    $.ajax({
                        url: url,
                        data: dataToSend,
                        type: &#39;GET&#39;,
                        success: function (dataReceived) {
                            //update control on View
                            var receivedValue = dataReceived.myResult;
                            $(&#39;YourControlIDToUpdate&#39;).val(receivedValue);
                        }
                    })
                });
        }
        &lt;/script&gt;
    }
**Issue still not solved yet**

</details>


# 答案1
**得分**: 1

@ahmed abdelaziz,请查看下面的代码

在获取值之前和之后,您需要编写一个URL

``` var valueofDropDown = $(this).val();
var url = '/WorkforceRequests/GetSelectedDropDownChanged?selectedValue='+valueofDropDown;

您是否检查过这个,因为我无法在您的代码中显示它

    //其他内容
}

_Layout.cshtml

@RenderSection("scripts", required: false)

英文:

@ahmed abdelaziz please check below code

you write a url before after get the value

                var valueofDropDown = $(this).val();  
var url = &#39;/WorkforceRequests/GetSelectedDropDownChanged?selectedValue=&#39;+valueofDropDown;

have you check this one because I am not able to show in your code

@section scripts {
//stuff
}

_Layout.cshtml

@RenderSection(&quot;scripts&quot;, required: false)

huangapple
  • 本文由 发表于 2023年2月6日 17:31:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定