0

I'm having a problem trying to get a ViewBage property value and pass it into a jQuery method. For example, in my code below, @ViewBag.CountResult have the value 1, and it is displayed in my browser, but when passed into the updateResultFooter method, it is like "" ! Don't get why. Down is my view. Any help ?

Thanks in advance

<div>
    <div>
        <span>Téléphone ?</span>
        <input id="idTxTel" type="text" name="txTelephone"/>

        <input id="idBnSearch" type="submit" value="Chercher" name="bnSearch"/>
    </div>
    @Html.Partial("_Result", Model)
</div>

<script type="text/javascript">
    var methodUrl = '@Url.Content("~/Search/GetReverseResult/")';

    $(document).ready(function (){
        updateResultFooter("@ViewBag.CountResult", "@ViewBag.PageNumber", "@ViewBag.PageCount");

        $("#idBnSearch").click(function (){           
            var telValue = $("#idTxTel").val();

            pageIndex = 0;
            doReverseSearch(telValue, pageIndex, methodUrl);
            updateResultFooter("@ViewBag.CountResult", 0, "@ViewBag.PageCount");
        });

        $("#bnNextPage").live("click", function (){
            var telValue = $("#idTxTel").val();

            pageIndex = pageIndex + 1;
            doReverseSearch(telValue, pageIndex, methodUrl);
            updateResultFooter("@ViewBag.CountResult", pageIndex, "@ViewBag.PageCount");
        });
    });

    function updateResultFooter(resultCount, pageIndex, pageCount){
        if (resultCount == '0')
            $("#resultFooter").hide();
        else 
        {
            $("#resultFooter").show();

            if (pageIndex == 0)
                $("bnPreviousPage").attr('disabled', 'disabled');
            else
                $("bnPreviousPage").removeAttr('disabled');

            if ((pageIndex + 1) == pageCount)
                $("bnNextPage").attr('disabled', 'disabled');
            else
                $("bnNextPage").removeAttr('disabled');
        }
    }

    function doReverseSearch(telValue, pageIdx, methodUrl){
    $.ajax({
            url: methodUrl,
            type: 'post',
            data: JSON.stringify({ Telephone: telValue, pageIndex: pageIdx }),
            datatype: 'json',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                $('#result').replaceWith(data);
            },
            error: function (request, status, err) {
                alert(status);
                alert(err);
            }
        });
    }
</script>

PS: I'm using this code to modify the display of the web page, depending on the result of a search made by the user.

SidAhmed
  • 2,332
  • 2
  • 25
  • 46
  • Is the problem with @ViewBag.CountResult? are you getting the values of @ViewBag.PageCount with this code? – Kundan Singh Chouhan Jul 18 '12 at 17:27
  • 1
    When you view source in your browser, is the first argument of `updateResultFooter` "1" or ""? – MrOBrian Jul 18 '12 at 17:28
  • The first argument is an empty string "", and this is for all the variables that I want to get from the ViewBag by jQuery – SidAhmed Jul 18 '12 at 18:59
  • I've tried to change my model, so it can have my desired values such as CountResult. So, I don't use the ViewBag anymore. but that didn't solve my problem. First, I had NullReferenceException when first loading the page in the line(updateResultFooter("@Model.CountResult", ...), I tried to check if my model is not null using (var jsModel = '@Html.Raw(Json.Encode(Model))';) but jsModel is always 'null' ! Why is that ? – SidAhmed Jul 19 '12 at 20:33

1 Answers1

0

I've changed the approach I was using. Now, I've changed my model so it contains the data I want to display, like (CountResult, PageCount). And it works fine now.

SidAhmed
  • 2,332
  • 2
  • 25
  • 46