Mi proyecto es un proyecto MVC c# Asp.NET. Quiero verificar si se ha cargado un archivo adjunto, si hay un archivo adjunto, muestra el div que contiene el archivo adjunto. Si no hay ningún archivo adjunto, quiero mostrar el div donde puede cargar un archivo adjunto. ¿Cómo cambiar el código Javascript para esto?
Mi código en la vista:
<tr> <th colspan="1" style="background-color:lightsteelblue"> Attachments: </th> <th colspan="11"> <div id="ifAttachment" style="display: none"> <input type="file" name="ImageFile2" /> </div> <div id="ifNOAttachment" style="display: none"> @{ var Files2 = (ViewBag.Files as string[]); if (Files2 != null && Files2.Any()) { foreach (var file in Files2) { <br /> @Html.ActionLink(file, "DownloadFile2", new { fileName2 = file }) <br /> } } else { <label>No File(s) to Download</label> } } </div> </th> </tr>Para el código Div/JS:
<script type="text/javascript"> $(function () { $("#ifAttachment").change(function () { if ($(this).val() == "Y") { $("#showsomething").show(); } }); });Ya tiene la lógica allí para representar HTML condicionalmente. Ni siquiera creo que necesites Javascript para esto. Solo tienes que mover algunos divs.
Me gusta organizar mis variables fuera de mi HTML.
@{var Files2 = ViewBag.Files as string[]}Luego, usaría la verificación if que ya tiene en su código para representar condicionalmente los elementos HTML.
<tr> <th colspan="1" style="background-color:lightsteelblue"> Attachments: </th> <th colspan="11"> @if (Files2 != null && Files2.Any()) { foreach (var file in Files2) { <br /> @Html.ActionLink(file, "DownloadFile2", new { fileName2 = file }) <br /> } } else { <label>No File(s) to Download</label> <div> <input type="file" name="ImageFile2" /> </div> } } </th> </tr>¡Ojalá eso funcione para ti!