查看页面无法识别加载中的null元素

时间:2012-11-17 11:47:37

标签: javascript asp.net-mvc

我有一个javascript函数,可以根据DropDown在我的页面中生成链接。我在两个地方检查javascript函数;当DropDown更改为属性“onchange”时,以及页面加载时。在viewbag上没有元素的情况下加载页面时会生成错误,我的javascript无法识别null元素以生成没有viewbag信息的链接。

<p>Pesquisar por: @Html.DropDownList("tipoPesquisa", ViewBag.DropDownPesquisa as SelectList, new { onchange = "alteraFiltro()" }) 



<div id="dadosFornecedor">
        @if (ViewBag.CurrentFornecedor != null)
        {
            <fieldset>
                <legend>@ViewBag.CurrentFornecedor.RazaoSocial</legend>
                <b>Razão Social:</b> @ViewBag.CurrentFornecedor.RazaoSocial <b>Endereço:</b> @ViewBag.CurrentFornecedor.Endereco
                @Html.Hidden("idFornecedor", new { IdFornecedor = ViewBag.CurrentFornecedor.IdFornecedor })
                <input type="button" class="btn" onclick="removerFornecedor();" value="Remover Fornecedor" />
                <br />
                <br />
            </fieldset>
            <br />

        }
    </div>


<script type="text/javascript">
    $(function () {

       alteraFiltro();
    });

    function removerFornecedor() {
        var div = $("#dadosFornecedor");
        var hidden = $("#idFornecedor");
        div.empty();

        div.append("<input type='hidden' name='deletarFornecedor' value='true' />")

    }

        function alteraFiltro() {
            var urlCodigo = $("#linkCodigo");
            var urlLancamento = $("#linkLancamento");
            var urlPagamento = $("#linkPagamento");
            var urlFornecedor = $("#linkFornecedor")
            var dropValue = $("#tipoPesquisa").val();
            var hidden = $("#idFornecedor");

            if (hidden.val() == "" || hidden.val() == null || hidden == null) {
                urlCodigo.attr("href", "saidasDiversas?sortOrder=@ViewBag.CodigoSortParm&currentFilter=@ViewBag.CurrentFilter&currentDrop=" + dropValue + "");
                urlLancamento.attr("href", "saidasDiversas?sortOrder=@ViewBag.DataLancamentoSortParm&currentFilter=@ViewBag.CurrentFilter&currentDrop=" + dropValue + "");
                urlPagamento.attr("href", "saidasDiversas?sortOrder=@ViewBag.DataPagamentoSortParm&currentFilter=@ViewBag.CurrentFilter&currentDrop=" + dropValue + "");
                urlFornecedor.attr("href", "saidasDiversas?sortOrder=@ViewBag.FornecedorSortParm&currentFilter=@ViewBag.CurrentFilter&currentDrop=" + dropValue + "")
            } else{
                urlCodigo.attr("href", "saidasDiversas?sortOrder=@ViewBag.CodigoSortParm&idFornecedor=@ViewBag.CurrentFornecedor.IdFornecedor&currentFilter=@ViewBag.CurrentFilter&currentDrop=" + dropValue + "");
                urlLancamento.attr("href", "saidasDiversas?sortOrder=@ViewBag.DataLancamentoSortParm&idFornecedor=@ViewBag.CurrentFornecedor.IdFornecedor&currentFilter=@ViewBag.CurrentFilter&currentDrop=" + dropValue + "");
                urlPagamento.attr("href", "saidasDiversas?sortOrder=@ViewBag.DataPagamentoSortParm&idFornecedor=@ViewBag.CurrentFornecedor.IdFornecedor&currentFilter=@ViewBag.CurrentFilter&currentDrop=" + dropValue + "");
                urlFornecedor.attr("href", "saidasDiversas?sortOrder=@ViewBag.FornecedorSortParm&idFornecedor=@ViewBag.CurrentFornecedor.IdFornecedor&currentFilter=@ViewBag.CurrentFilter&currentDrop=" + dropValue + "")
            }

        }


    </script>

1 个答案:

答案 0 :(得分:0)

如果问题是由ViewBag.CurrentFornecedor在首次加载视图时没有值引起的,请尝试按以下方式更改您的javascript:

 var hidden = $("#idFornecedor");

 if (hidden.val() == "" || hidden.val() == null || hidden == null) {

 // ...

 }
 else {

    urlCodigo.attr("href", "saidasDiversassortOrder=@ViewBag.CodigoSortParm&idFornecedor=" 
         + @{ViewBag.CurrentFornecedor != null ? ViewBag.CurrentFornecedor.IdFornecedor : -1} 
        + "&currentFilter=@ViewBag.CurrentFilter&currentDrop=" + dropValue + "");

    // ...

}