Have a view with 2 drop-downs the 2nd one disabled by default. When selection is made in first drop-down, it is supposed to both call an action in my controller to retrieve list of values for 2nd drop-down, then use simple scripting to clear the 2nd drop-down, enable the 2nd drop-down and fill it with the results.
When I make a selection from the first drop-down, the 2nd drop-down is enabled but no values appear. Using Ctrl-shift-i I see the error TypeError: right-hand side of 'in' should be an object, got string.
But I'm using a List of values not a string variable...
I have verified in SQL manager that the SQL query works for all 3 different options available in first drop-down.
Added alert(attValues); into $.post. It is resulting in an entire HTML doc being shown in the alert that goes about really long way of saying 404 page not found.
The important line seeming to be:
<form method="post" action="../../../Error_404.aspx?selectedOption=SKU_SelectableAttribute_2" id="error404">
Controller
[HttpPost]
public ActionResult GetAttributeValues(string selectedOption)
{
JsonResult result = new JsonResult();
if (selectedOption != null)
{
string sql = "SELECT Title FROM BL_Attribute (NOLOCK) WHERE BL_Attribute.DeleteFlag = '0' AND AttributeGroupName = '" + selectedOption + "'";
using (SqlCommand selectAttValues = new SqlCommand(sql, P21SqlConnection))
{
using (SqlDataReader reader = selectAttValues.ExecuteReader())
{
List<string> attValues = new List<string>();
while (reader.Read())
{
attValues.Add(reader["Title"].ToString());
}
return Json(attValues, JsonRequestBehavior.AllowGet);
}
}
}
return Json(new { Success = "false" });
}
View
@using System.Data
@model Product
@{
ViewBag.Title = "Attributes";
Layout = "~/Views/Shared/_VisualRuleLayout.cshtml";
var listAttGroups = new List<SelectListItem>
{
new SelectListItem { Text = "SKU_Color", Value = "SKU_Color"},
new SelectListItem { Text = "SKU Select Att 1", Value = "SKU_SelectableAttribute_1"},
new SelectListItem { Text = "SKU Select Att 2", Value = "SKU_SelectableAttribute_2"}
};
}
@section scripts{
<script>
$(function () {
$("#ApplyGroup").change(function () {
var option = $(this).val();
$("#AttributeValue").empty();
$("#AttributeValue").prop('disabled', false);
var url = "KCDA_PrdGrp_Attributes/GetAttributeValues?selectedOption=" + option;
$.post(url, function (attValues) {
$.each(attValues, function (i, attValue) {
$("#AttributeValue").append($('<option></option>').val(attValue).html(attValue));
});
});
});
});
</script>
}
<center><h3>Edit Attributes</h3></center>
@using (Html.BeginForm("Return", "KCDA_PrdGrp_Attributes", FormMethod.Post, new { name = "Return" }))
{
@Html.DropDownList("ApplyGroup", listAttGroups, new { @id = "ApplyGroup", @class = "form-control" })
@Html.DropDownList("AttributeValue", new List<SelectListItem>(), new { @id = "AttributeValue", @class = "form-control", disabled = "disabled" })
}