我有一个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¤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>
答案 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}
+ "¤tFilter=@ViewBag.CurrentFilter¤tDrop=" + dropValue + "");
// ...
}