0

我有一个 javascript 函数,它基于 DropDown 在我的页面中生成链接。我在两个地方检查 javascript 函数;当 DropDown 使用属性“onchange”更改时,以及页面加载时。当页面在 viewbag 上没有元素的情况下加载时会生成错误,我的 javascript 无法识别空元素以生成没有 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>
4

1 回答 1

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 + "");

    // ...

}
于 2012-11-17T16:59:04.437 回答