I am currently working with a JavaScript function that generates links on my webpage based on the user's selection from a DropDown menu. I have implemented checks for this JavaScript function in two scenarios: when the user selects an option from the DropDown (using the "onchange" attribute) and when the page is initially loaded. The problem arises when the page is loaded without any element in the viewbag, causing my JavaScript to fail in generating links in the absence of necessary information from the viewbag.
<p>Search by: @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="Remove Supplier" />
<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) {
// Update the links accordingly
} else {
// Update the links accordingly
}
}
</script>