我的编码如下。
我无法获得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;
}`
答案 0 :(得分:0)
在你的控制器中你有这一行:
result = "<select name='AreaDetails_code" + idx2 + "' id='AreaDetails_code" + idx2 + "'>";
其中idx2
是变量int,当您尝试使用jquery获取值时:
var _selVal = $('#AreaDetails_code').val();
您没有在选择器中附加此idx2
值,因此jquery会分配null值,因为它无法找到它。
如果您从控制器中删除此占位符,那么您最终会得到具有相同ID的多个元素...没有bueno。您必须找到更好的命名约定或以某种方式将该值传递给您的view / javascript,以便它可以知道哪个元素可以获取值。
希望有所帮助。