I am doing a web application in C # ASP.NET MVC5, I need in the view to validate a dropdownlist that is not empty, and when it is full send the data to the controller.
JavaScript
function Validar() {
var pais_id = document.getElementById("id_pais ").selectedIndex;
if (pais_id == 0) {
document.getElementById("id_pais ").focus();
alert('[ERROR] El campo Test es requerido.');
return false;
}
else
{
document.registros.action = @Url.Action("Index", "Registros");
document.registros.submit();
}
}
the view
@using (Html.BeginForm(null, null, FormMethod.Post, new { name = "registros", id = "registros", enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.pais)
<div class="col-md-10">
@Html.DropDownList("id_stb", null, "Seleccione un Pais")
</div>
@if (ViewBag.Message != null)
{
@ViewBag.Message
}
</div>
When I try only the alert
if it works, but when I add the part of the submit
, the method does not work anymore, not even the alert
. I guess I have the URL wrong to the controller.
I'm new to this from C # and asp.net, thanks in advance.