【问题标题】:How to get the DDLlist Selected Value in MVC C# Controller如何在 MVC C# 控制器中获取 DDLlist 选定值
【发布时间】:2013-12-06 06:46:33
【问题描述】:

我的编码如下。

我无法获取 AreaDetails_code 下拉列表选择值的选择值。

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

在我的 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>

在我的 Controller.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;
        }`

【问题讨论】:

  • 使用stringbuilder类通过TagBuilder生成html标签
  • 怎么做?请为我写信。谢谢
  • 试一试:结果 += "
  • result += "
  • 那么,这行的问题是:var _selVal = $('#AreaDetails_code').val();,你得到 _selVal 总是空的吗?请确认

标签: c# asp.net-mvc drop-down-menu ddl selected


【解决方案1】:

在你的控制器中你有这行:

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

idx2 是一个变量 int,当您尝试使用 jquery 获取值时:

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

您没有在选择器中附加此 idx2 值,因此 jquery 分配 null 的值,因为它找不到它。

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

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2018-04-27
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    相关资源
    最近更新 更多