Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MVC#控制器中获取DDLIST选择值_C#_Asp.net Mvc_Drop Down Menu_Ddl_Selected - Fatal编程技术网

如何在MVC#控制器中获取DDLIST选择值

如何在MVC#控制器中获取DDLIST选择值,c#,asp.net-mvc,drop-down-menu,ddl,selected,C#,Asp.net Mvc,Drop Down Menu,Ddl,Selected,我的编码如下。 我无法获取AreaDetails\u code dropdownlist选定值的选定值 我只得到了空值。请帮帮我。提前谢谢 在我的Create.cshtml中 <div id="hsp-planner"></div> <script type="text/javascript"> $('#butGenerate').click(`function generateTimeLine() { $('#hsp

我的编码如下。

我无法获取AreaDetails\u code dropdownlist选定值的选定值

我只得到了空值。请帮帮我。提前谢谢

在我的Create.cshtml中

<div id="hsp-planner"></div>

<script type="text/javascript">
$('#butGenerate').click(`function generateTimeLine() {
                    $('#hsp-planner').html('');

                    var _location = $('#pAreaInfoId').val();
                    var _staff = $('#pStaff').val();

                    var _selVal = $('#AreaDetails_code').val();



                    $.ajax({
                        url: '@Url.Action("GetAjaxPlanner", "HourlyShiftPlanner")',

                        data: { location: _location, staff: _staff, selectval: _selVal },
                        //dataType: 'json',
                        success: function (data) {
                            $('#hsp-planner').html(data);

                        },
                        failure: function (response) {
                            alert(response);
                        }
                    });
                });

    });
`

</script>

$('#butGenerate')。单击(`function generateTimeLine(){
$('#hsp planner').html('');
var#u location=$('#pAreaInfoId').val();
var _staff=$('#pStaff').val();
var_selVal=$(“#区域详细信息_代码”).val();
$.ajax({
url:'@url.Action(“GetAjaxPlanner”、“HourlyShift Planner”),
数据:{location:{u location,staff:{u staff,selectval:{u selVal},
//数据类型:“json”,
成功:功能(数据){
$('hsp planner').html(数据);
},
故障:功能(响应){
警报(响应);
}
});
});
});
`
在我的控制器中。cs

public string GetAjaxPlanner()

{

string locationId = Request.QueryString["location"];
            string staffId = Request.QueryString["staff"];

            string selectVal = Request.QueryString["selectval"];

            //String str = (String)req.getParameter("AreaDetails_code");


            if (locationId != null && locationId != "")
            {
                List<ShiftAllocation> saList = new ShiftAllocationRepository().FindByAreaInfoId(int.Parse(locationId));


                if (saList.Count > 0)
                {
                    string result = "";

                    result = "<table>";

                    result += "<tr>";
                    result += "<td>Time Period<br/>(From-To)</td>";
                    result += "<td>Duty Location</td>";
                    //result += "<td>Remark</td>";
                    result += "<td>Staff</td>";
                    result += "</tr>";

                    int i = 1;
                    foreach (ShiftAllocation sa in saList)
                    {
                        HourlyShiftPlanner objHsp = null;

                        string dutyLocation = "";
                        string remark = "";

                        if (staffId != null && staffId != "")
                        {
                            objHsp = new HourlyShiftPlannerRepository().FindStaff(int.Parse(staffId), int.Parse(locationId), sa.ShiftAllocationId);
                        }

                        if (objHsp != null)
                        {
                            dutyLocation = objHsp.DutyLocation;
                            remark = objHsp.Remark;
                        }

                        result += "<tr>";
                        result += "<td>" + sa.FromShift.ToString("HH:mm") + "-" + sa.ToShift.ToString("HH:mm") + "</td>";
                        result += "<td>" + this.GetLocDetailsListWithDDL(i) + "</td>";


                        result += "</tr>";
                        result += "<input type='hidden' name='hidSAId" + i + "'  id='hid-sa-id" + i + "' value='" + sa.ShiftAllocationId + "'/>";

                        i++;
                    }

                    result += "</table>";

                    return result;
                }
            }

            return null;
        }




private string GetLocDetailsListWithDDL(int idx2)
        {
            List<AreaInfoDetails> _areaDetailsList = db.AreaInfosDetails.ToList();

            if (_areaDetailsList.Count == 0) return "";

            string result = "";

            result = "<select name='AreaDetails_code" + idx2 + "' id='AreaDetails_code" + idx2 + "'>";

            result += "<option value=>--Select item--</option>";

            foreach (AreaInfoDetails _ad in _areaDetailsList)
            {

                result += "<option value='" + _ad.AreaInfoDetailsId + "'>" + _ad.AreaDetailsCode + " </>";
            }

            result += "</select>";

            //result += "<input type=submit id=submit value=Submit  </>";

            return result;
        }`
公共字符串GetAjaxPlanner()
{
string locationId=Request.QueryString[“location”];
string staffId=Request.QueryString[“staff”];
string selectVal=Request.QueryString[“selectVal”];
//String str=(String)req.getParameter(“AreaDetails_code”);
如果(locationId!=null&&locationId!=“”)
{
List saList=new ShiftAllocationRepository().FindByAreaInfoId(int.Parse(locationId));
如果(saList.Count>0)
{
字符串结果=”;
结果=”;
结果+=”;
结果+=“时间段
(从到)”; 结果+=“工作地点”; //结果+=“备注”; 结果+=“工作人员”; 结果+=”; int i=1; foreach(saList中的ShiftAllocation sa) { HourlyShift Planner objHsp=null; 字符串dutyLocation=“”; 字符串备注=”; 如果(staffId!=null&&staffId!=“”) { objHsp=new HourlyShiftPlannerRepository().FindStaff(int.Parse(staffId)、int.Parse(locationId)、sa.ShiftAllocationId); } 如果(objHsp!=null) { dutyLocation=objHsp.dutyLocation; 备注=对象备注; } 结果+=”; 结果+=“+sa.FromShift.ToString(“HH:mm”)+”-“+sa.ToShift.ToString(“HH:mm”)+”; 结果+=“”+this.GetLocDetailsListWithDDL(i)+”; 结果+=”; 结果+=”; i++; } 结果+=”; 返回结果; } } 返回null; } 私有字符串GetLocDetailsListWithDDL(int idx2) { 列表_areaDetailsList=db.AreaInfosDetails.ToList(); 如果(_areaDetailsList.Count==0)返回“”; 字符串结果=”; 结果=”; 结果+=“--选择项目--”; foreach(区域信息详情列表中的广告) { 结果+=”+_广告区域详细信息代码+”; } 结果+=”;
//结果+=“在控制器中,您有以下行:

result = "<select name='AreaDetails_code" + idx2 + "' id='AreaDetails_code" + idx2 + "'>";
您没有在选择器中追加这个
idx2
值,因此jquery分配null值,因为它找不到它

如果您从控制器中删除此占位符,那么您将得到多个id相同的元素…没有bueno。您必须找到更好的命名约定,或者以某种方式将该值传递给视图/javascript,以便它知道要获取哪个元素的值


希望对您有所帮助。

使用stringbuilder类生成带有TagBuilder的html标记如何操作?请为我写一封信。谢谢:result++=“ad.AreaDetailsCode+”;result++=“ad.AreaDetailsCode+”;我按照您的建议进行了更改。不起作用。那么,这行中的问题是:
var\u selVal=$(“\AreaDetails\U code”)。val();
,您得到的选择值始终为空?请确认
var _selVal = $('#AreaDetails_code').val();