1

I am trying to collect selected values from two drop down boxes with multiselect. Then send the data with ajax as strings to a controller to use in stored procedure for a partial view. I can see that the strings are being sent, but the SQL is not running. Can anyone help please, I have trawled everywhere but can't find a similar problem. Thank you.

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());
    }

STORED PROCEDURE

SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO

ALTER PROCEDURE [dbo].[spPropertyListXML]
@SuburbXML XML -- sd1: defined as xml , @AreaXML XML -- sd1: defined as xml

AS BEGIN SET FMTONLY OFF

SET NOCOUNT ON
BEGIN TRY

-- create temp

    CREATE TABLE #TempSuburb                        (SuburbId       INT)
    CREATE TABLE #TempArea                          (AreaId         INT)

-- load temp

    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)

-- use data in 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 temp

    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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.