Javascript . 您需要使用SqlParameter,在JSON的第4行第1列有一个非常重要的问题SyntaxError:JSON.parse:unexpected字符data@Bert-也许可以尝试注销您的attValues响应。可能输入了空值或空值[1,2,3]是
Javascript . 您需要使用SqlParameter,在JSON的第4行第1列有一个非常重要的问题SyntaxError:JSON.parse:unexpected字符data@Bert-也许可以尝试注销您的attValues响应。可能输入了空值或空值[1,2,3]是,javascript,c#,asp.net-mvc,razor,Javascript,C#,Asp.net Mvc,Razor,. 您需要使用SqlParameter,在JSON的第4行第1列有一个非常重要的问题SyntaxError:JSON.parse:unexpected字符data@Bert-也许可以尝试注销您的attValues响应。可能输入了空值或空值[1,2,3]是无效的JSON,并导致一个非常类似的错误。这主意不错,您建议我怎么做?尽管看起来很奇怪,无论我选择哪3个选项,我都会得到相同的错误。。。有时我认为java代码甚至没有成功地使用$.post调用actionresult-再次感谢您的帮助;在var
. 您需要使用
SqlParameter
,在JSON的第4行第1列有一个非常重要的问题SyntaxError:JSON.parse:unexpected字符data@Bert-也许可以尝试注销您的attValues
响应。可能输入了空值或空值<代码>[1,2,3]是无效的JSON,并导致一个非常类似的错误。这主意不错,您建议我怎么做?尽管看起来很奇怪,无论我选择哪3个选项,我都会得到相同的错误。。。有时我认为java代码甚至没有成功地使用$.post调用actionresult-再次感谢您的帮助;在var result=JSON.parse(attValues)之前,@Bert-很高兴能帮助您找到需要的地方。将来,使用console.log(attValues)
将提供比警报更多的信息(它将显示在浏览器的控制台中,通常通过按键盘上的F12键可以找到)。
[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" });
}
@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" })
}
$.post(url, function (attValues) {
var result = JSON.parse(attValues);
if( result.Success && result.Success == "false" ) return;
$.each(result, function (i, attValue) {
$("#AttributeValue").append($('<option></option>').val(attValue).html(attValue));
});
});
$("#ApplyGroup").change(function () {
var option = $(this).val();
$("#AttributeValue").empty();
$("#AttributeValue").prop('disabled', false);
var url = "GetAttributeValues?selectedOption=" + option;
$.post(url, function (attValues) {
$.each(attValues, function (i, attValue) {
$("#AttributeValue").append($('<option></option>').val(attValue).html(attValue));
});
});
});