我有一个 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¤tFilter=@ViewBag.CurrentFilter¤tDrop=" + dropValue + "");
urlLancamento.attr("href", "saidasDiversas?sortOrder=@ViewBag.DataLancamentoSortParm¤tFilter=@ViewBag.CurrentFilter¤tDrop=" + dropValue + "");
urlPagamento.attr("href", "saidasDiversas?sortOrder=@ViewBag.DataPagamentoSortParm¤tFilter=@ViewBag.CurrentFilter¤tDrop=" + dropValue + "");
urlFornecedor.attr("href", "saidasDiversas?sortOrder=@ViewBag.FornecedorSortParm¤tFilter=@ViewBag.CurrentFilter¤tDrop=" + dropValue + "")
} else{
urlCodigo.attr("href", "saidasDiversas?sortOrder=@ViewBag.CodigoSortParm&idFornecedor=@ViewBag.CurrentFornecedor.IdFornecedor¤tFilter=@ViewBag.CurrentFilter¤tDrop=" + dropValue + "");
urlLancamento.attr("href", "saidasDiversas?sortOrder=@ViewBag.DataLancamentoSortParm&idFornecedor=@ViewBag.CurrentFornecedor.IdFornecedor¤tFilter=@ViewBag.CurrentFilter¤tDrop=" + dropValue + "");
urlPagamento.attr("href", "saidasDiversas?sortOrder=@ViewBag.DataPagamentoSortParm&idFornecedor=@ViewBag.CurrentFornecedor.IdFornecedor¤tFilter=@ViewBag.CurrentFilter¤tDrop=" + dropValue + "");
urlFornecedor.attr("href", "saidasDiversas?sortOrder=@ViewBag.FornecedorSortParm&idFornecedor=@ViewBag.CurrentFornecedor.IdFornecedor¤tFilter=@ViewBag.CurrentFilter¤tDrop=" + dropValue + "")
}
}
</script>