Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

286
Visualizações
In ASP.net MVC razor, i have to give a alert message based on the dropdown selected value. the following is the code

if the user selects "Fresher" from the dropdown, and if he doesn't enter any value in Company detail. it should give a alert message.

what is the javascript condtion to get the alert message ?

any help is appriciable. Thanks

The razor code is :

<div class="form-group">

  <label>Current Status</label><span class="text text-danger">*</span> @Html.DropDownList("employmentStatus", new SelectList(new Dictionary
  <int, string> { { 0, "Current Status" }, { 1, "Fresher" }, { 2, "Employed" }, { 3, "Un-Employed" } }, "Key", "Value"), new { @class = "form-control", @id = "employmentStatus" }) @Html.ValidationMessageFor(model => model.employmentStatus, "", new { @class = "text-danger"
    })
</div>

<div class="form-group">

  <label>Company Detail</label><span class="text text-danger">*</span> @Html.EditorFor(model => model.currentCompany, new { htmlAttributes = new { @class = "form-control",@id= "currentCompany" } }) @Html.ValidationMessageFor(model => model.currentCompany,
  "", new { @class = "text-danger" })
</div>

The javascript code is :

if ($("#employmentStatus").val() == "Fresher" || ("#employmentStatus").val() == "Employed" && ("#currentCompany").val() == "" || ("#currentCompany").val() == null) {
  alert('Please select Current Company Name')
  return false;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Assumption:

You have a button or form event that firing onSubmit method before posting data to API.

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { @id = "form" }))
{
  // Form inputs
  
  <input type="submit" value="Submit" onclick="return onSubmit()" class="btn btn-primary">
}

Since you specify the <option> value as Dictionary key, while label text as Dictionary value.

@Html.DropDownList("employmentStatus",
         new SelectList(new Dictionary<int, string>
         {
                { 0, "Current Status" },
                { 1, "Fresher" },
                { 2, "Employed" },
                { 3, "Un-Employed" }
         }, "Key", "Value"),
         new { @class = "form-control", @id = "employmentStatus" })

Rendered HTML

<select class="form-control" id="employmentStatus" name="employmentStatus">
  <option value="0">Current Status</option>
  <option value="1">Fresher</option>
  <option value="2">Employed</option>
  <option value="3">Un-Employed</option>
</select>

Hence, you need to check the selected option's value with Dictionary key and currentCompany input element cannot be empty string or null.

if ($("#employmentStatus").val() == "1" 
  && ($("#currentCompany").val() == "" || $("#currentCompany").val() == null)) {
  alert('Please select Current Company Name');
  return false;
}

OR

if ($("#employmentStatus option:selected")[0].text == "Fresher"
  && ($("#currentCompany").val() == "" || $("#currentCompany").val() == null)) {
  alert('Please select Current Company Name');
  return false;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda