Я пытаюсь собрать выбранные значения из двух выпадающих списков с множественным выбором. Затем отправьте данные с ajax в виде строк в контроллер для использования в хранимой процедуре для частичного просмотра. Я вижу, что строки отправляются, но SQL не работает. Может кто-нибудь помочь, пожалуйста, я везде трал, но не могу найти аналогичные проблемы. Спасибо.
VIEW
@Html.AntiForgeryToken()
<div class="PropLandingGrid">
<div class="PLAND_A1">
<select name="SuburbDDL" id="SuburbDDL" multiple="multiple"></select>
</div>
<div class="PLAND_A3">
<select name="AreaDDL" id="AreaDDL" multiple="multiple"></select>
</div>
<div class="PLAND_D1"><button type="button" id="btnSearch" class="btn-det15035">Show</button></div>
</div>
<div id="PropContainer"></div>
$(document).ready(function ()
{
var SuburbDDL = $('#SuburbDDL');
var AreaDDL = $('#AreaDDL');
var AreaDet = null;
var AreaQ = null;
var SuburbDet = 1;
var SuburbQ = null;
AreaDDL.multipleSelect();
// suburb
$.ajax({
url: '/tProps/GetSuburbList',
method: 'post',
dataType: 'json',
success: function (data) {
SuburbDDL.append($('<option/>', { value: -1, text: "Select Suburb" }));
$(data).each(function (PropLanding2, item) {
SuburbDDL.append($('<option/>', { value: item.SuburbID, text: item.Suburb }));
});
$('select option:contains("Hout Bay")').prop('selected', true);
SuburbDDL.multipleSelect();
SuburbDet = 1;
}
});
// area
$.ajax({
url: '/tProps/GetAreaListJS',
method: 'post',
data: { SuburbID: '1' },
dataType: 'json',
success: function (data) {
AreaDDL.empty();
AreaDDL.append($('<option/>', { value: -1, text: "Select Area" }));
AreaDDL.attr("disabled", false);
$(data).each(function (PropLanding2, item) {
AreaDDL.append($('<option/>', { value: item.AreaID, text: item.Area }));
});
AreaDDL.multipleSelect();
AreaDet = null;
}
});
SuburbDDL.change(function () {
if ($(this).val() == '-1') {
AreaDDL.empty();
AreaDDL.append($('<option/>', { value: -1, text: "Select Area" }));
AreaDDL.val('-1');
AreaDDL.attr('disabled', true);
}
else {
AreaDDL.attr("disabled", false);
$.ajax({
url: '/tProps/GetAreaListJS',
method: 'post',
data: { SuburbID: $(this).val() },
dataType: 'json',
success: function (data) {
AreaDDL.empty();
AreaDDL.append($('<option/>', { value: -1, text: "Select Area" }));
AreaDDL.attr("disabled", false);
$(data).each(function (PropLanding2, item) {
AreaDDL.append($('<option/>', { value: item.AreaID, text: item.Area }));
});
}
});
SuburbDet = SuburbDDL.multipleSelect('getSelects');
}
});
AreaDDL.change(function ()
{
if ($(this).val() == '-1') {
AreaDet = null;
}
else
{
AreaDet = AreaDDL.multipleSelect('getSelects');
}
});
$("#btnDets").click(function () {
alert("Suburb: " + SuburbDet + ", Area: " + AreaDet)
});
$("#btnSearch").click(function () {
SuburbDet1 = (String(SuburbDet).replace(/,/g, "</SuburbId></Detail><Detail><SuburbId>"));
SuburbDet2 = "<Root><Detail><SuburbId>" + SuburbDet1 + "</SuburbId></Detail></Root>"
console.log(SuburbDet2);
AreaDet1 = (String(AreaDet).replace(/,/g, "</AreaId></Detail><Detail><AreaId>"));
AreaDet2 = "<Root><Detail><AreaId>" + AreaDet1 + "</AreaId></Detail></Root>"
console.log(AreaDet2);
$.ajax({
url: '/tProps/p_PropList2',
contentType: 'application/html',
type: 'GET',
data:
{
'SuburbData': SuburbDet2, 'AreaData': AreaDet2
},
dataType: 'html',
success: function (result) {
$('#PropContainer').html(result);
alert("success");
},
error: function () {
alert("something went wrong");
}
})
});
CONTROLLER
public PartialViewResult p_PropList2(String SuburbData, String AreaData, string sortBy)
{
String StringSQL = "DECLARE @SuburbXML VARCHAR(MAX) DECLARE @AreaXML VARCHAR(MAX) SET @SuburbXML = " + SuburbData + " SET @AreaXML = " + AreaData + " EXEC spPropertyListXML @SuburbXML, @AreaXML";
var datta = db.Database.SqlQuery<spPropertyListXML_Result>(StringSQL).ToList();
return PartialView("p_PropList2", datta.ToList());
}
ХРАНЕННАЯ ПРОЦЕДУРА
SET ANSI_NULLS ON
ИДТИ
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo]. [SpPropertyListXML]
@SuburbXML XML - sd1: определен как xml
, @AreaXML XML - sd1: определено как xml
AS
НАЧАТЬ
SET FMTONLY OFF
SET NOCOUNT ON
BEGIN TRY
- создать темп
CREATE TABLE #TempSuburb (SuburbId INT)
CREATE TABLE #TempArea (AreaId INT)
- температура нагрузки
INSERT INTO #TempSuburb(SUburbId)
SELECT Suburb.ID.query('SuburbId').value('.','INT')
FROM @SuburbXML.nodes('/Root/Detail') AS Suburb(ID)
INSERT INTO #TempArea(AreaId)
SELECT Area.ID.query('AreaId').value('.','INT')
FROM @AreaXML.nodes('/Root/Detail') AS Area(ID)
- использовать данные в temp
SELECT tArea.Area, tArea.AreaID, tSuburb.SuburbID, tSuburb.Suburb, tProp.Erf, tProp.ErfSq, tProp.Num
FROM tprop
INNER JOIN tArea ON tArea.AreaID = tprop.area
INNER JOIN tSuburb ON tSuburb.SuburbID = tarea.SuburbID
WHERE (NOT EXISTS (SELECT TOP 1 1 FROM #TempSuburb)
OR tSuburb.SuburbID IN (SELECT a.SuburbId FROM #TempSuburb a)
)
AND (NOT EXISTS (SELECT TOP 1 1 FROM #TempArea)
OR tArea.AreaID IN (SELECT a.AreaId FROM #TempArea a)
)
- температура спада
DROP TABLE #TempSuburb
DROP TABLE #TempArea
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK TRAN
DECLARE @ERROR_MESSAGE VARCHAR(MAX)
DECLARE @ERROR_SEVERITY INT
SELECT @ERROR_MESSAGE = ERROR_MESSAGE()
, @ERROR_SEVERITY = ERROR_SEVERITY()
RAISERROR(@ERROR_MESSAGE, @ERROR_SEVERITY, 1)
END CATCH
END