我试图过滤掉之前下拉列表中的下拉列表,但json将结果传递为null。在调试时的jquery中,它显示了businessunitCd和facilityCd的值,但在控制器中它显示为null。
当我尝试调试时,它表示我的SQLPersistor在此行SqlDataReader dr = cmd.ExecuteReader();
上出错,并显示错误
他们找不到
@FAC_CD
我的控制器:
public JsonResult GetProcessShown(string businessUnitCd, string facilityCd)
{
IEnumerable<SelectListItem> processListItems = new List<SelectListItem>();
SQLPersistor p = new SQLPersistor(EADConnString);
List<ProcessShown> processes = p.GetProcessShown(businessUnitCd, facilityCd);
ProcessShown defaultProcessShown = new ProcessShown();
defaultProcessShown.ProcessCd = string.Empty;
defaultProcessShown.ProcessDesc = "Select Process...";
processes.Insert(0, defaultProcessShown);
processListItems = (from pr in processes
select new SelectListItem
{
Text = pr.ProcessDesc,
Value = pr.ProcessCd
});
return Json(processListItems, JsonRequestBehavior.AllowGet);
}
我的SQLPersistor:
public List<ProcessShown> GetProcessShown(string businessUnitCd, string facilityCd)
{
List<ProcessShown> Processes = new List<ProcessShown>();
if (businessUnitCd != string.Empty && facilityCd != string.Empty)
{
using (SqlConnection cnn = new SqlConnection(connString))
{
cnn.Open();
string sql = "[Environmental].[GetProcessShown]";
using (SqlCommand cmd = new SqlCommand(sql, cnn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@BUSUNIT", SqlDbType.VarChar).Value = businessUnitCd;
cmd.Parameters.Add("@FAC_CD", SqlDbType.VarChar).Value = facilityCd;
SqlDataReader dr = cmd.ExecuteReader();
using (DataTable dt = new DataTable())
{
dt.Load(dr);
foreach (DataRow row in dt.Rows)
{
ProcessShown ps = new ProcessShown(row["PROCESS_CD"].ToString(), !Convert.IsDBNull(row["PROCESS_NAME"]) ? row["PROCESS_NAME"].ToString() : string.Empty);
Processes.Add(ps);
}
}
}
}
}
return Processes;
}
我的jquery:
$("#Facility").change(function () {
$("#ProcessShown").empty();
$("#Aspects").empty();
$("#AspectsCategory").empty();
$("#Activity").empty();
//document.getElementById("OnCallContactMessage").style.display = "none";
// Incase theyve double clicked a cause and changed their mind
document.getElementById("BusinessUnit").required = true;
document.getElementById("Facility").required = true;
document.getElementById("ProcessShown").required = true;
document.getElementById("Aspects").required = true;
document.getElementById("AspectCategoryDiv").required = true;
document.getElementById("ActivityName").required = true;
var ProcessOptions = {};
ProcessOptions.url = $("ProcessDiv").data('url');
ProcessOptions.type = "POST";
ProcessOptions.data = JSON.stringify({ businessUnitCd: $("#BusinessUnit").val(), facilityCd: $("#Facility").val() });
ProcessOptions.datatype = "json";
ProcessOptions.contentType = "application/json";
ProcessOptions.success = function (processes) {
if (processes.length > 0) {
for (var i = 0; i < processes.length; i++) {
$("#ProcessShown").append(new Option(processes[i].Text, processes[i].Value));
}
}
};
ProcessOptions.error = function () { alert("Error occurred while getting Processes.") };
$.ajax(ProcessOptions);
});
我的观点:
@* Processes Shown - Dropdown *@
<div class="row" style="padding-top:10px">
<div class="col-xs-3" style="padding-top:8px">
@Html.LabelFor(model => model.ProcessShown, new { @class = "group-label" })
</div>
<div class="col-xs-8" id="ProcessDiv" data-url="@Url.Action("GetProcessShown", "Aspects")">
@Html.DropDownListFor(model => model.ProcessShown, Model.ProcessList, new { @class = "form-control req-field", @required = "required" })
</div>
</div>
我的存储过程:
ALTER PROCEDURE [Environmental].[GetProcessShown]
@FAC_CD VarChar(255),
@BUSUNIT VarChar(255)
AS
BEGIN
SET NOCOUNT ON;
SELECT CDTBL_PROCESSES.PROCESS_CD, PROCESS_NAME
FROM(SELECT DISTINCT PROCESS_CD FROM ENVIRN_BU_PROCESS_FAC WHERE FAC_CD = @FAC_CD AND BUS_UNIT_CD = @BUSUNIT) PROCESSES
JOIN CDTBL_PROCESSES
ON CDTBL_PROCESSES.PROCESS_CD = PROCESSES.PROCESS_CD
AND OBSLT_EST IS NULL
END
答案 0 :(得分:1)
当您使用stored procedure
您的SP第一个参数是@FAC_CD
。所以你应该改变cmd.Parameters.Add
参数顺序。
喜欢这个
cmd.Parameters.Add("@FAC_CD", SqlDbType.VarChar).Value = facilityCd;
cmd.Parameters.Add("@BUSUNIT", SqlDbType.VarChar).Value = businessUnitCd;
答案 1 :(得分:0)
很可能是因为你在ajax.data部分传递参数的方式。之前有很多关于这个问题的问题。首先,您可以看到以下链接:
Ajax Post Parameter is Always Null in MVC app
简而言之,请尝试以下方法之一:
但是,我已经尝试了上面的原始代码并且没有任何问题。
无论如何,希望它有所帮助。
答案 2 :(得分:0)
您发送的数据实际上是一个复合对象,而不是两个独立的字符串。您可以通过在服务器端创建模型来匹配它:
public class ProcessRequest
{
public string Facility { get; set; }
public string BusinessUnit { get; set; }
}
控制器方法签名会相应更改,并且必须添加HttpPost属性,因为您通过POST发送HTTP请求。
[HttpPost]
public JsonResult GetProcessShown(ProcessRequest requestData) {}
对于Sql异常,当将NULL值作为存储过程中的参数传入时,将触发错误。