Search code examples
javascripthtmlrazorasp.net-mvc-5

using `@using (html.beginform())` JavaScript is Disabled?


There is a problem with posting this form when JavaScript is disabled. Of course, when I delete @using (html.beginform()) it fixes the problem.

@using (Html.BeginForm("AddTour", "AdminTour", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
  <div class="row">
  <div class="form-gro`enter code here`up">
  <form id="creation" name="creation" onchange="handleSelect()">
  <div class="col-md-6">
  @{
     List<Airlines> AirlineId = ViewBag.AirlinesId;
  }
  <select id="Airline" name="Airline" disabled>
  <option value="">- انتخاب کنید -</option>
  @foreach (Airlines c in AirlineId)
  {
      <option value="@c.Id">@c.Name</option>
  }
  </select>
  </div>
  <div class="col-md-6 ">
  <select id="TravelType" name="TravelType">
  <option value="">- انتخاب کنید -</option>
  <option value="A">1</option>
  <option value="B">2</option>
  <option value="C">3</option>
  </select>
  </div>
  </form>
  <script>
  function handleSelect() {
      if (document.getElementById('TravelType').value == 'A') {
           document.getElementById('Airline').disabled = false;
      } else {
           document.getElementById('Airline').disabled = true;
      }
   }
   </script>
   </div>
   </div>
}

In my opinion, the problem is <form>.

How can I change onchange ="handleSelect()" or <select> to change the filter?


Solution

  • You can use Javascript within <form> elements without issue (assuming Javascript is enabled) and the same holds true for one generated using Html.BeginForm().

    If you are expecting an environment that will have Javascript disabled, then you might consider explicitly performing a post when your filtering element changes and displaying the updated version (i.e. with your element disabled) to reflect these changes.

    You might also consider simply not disabling the element at all and just handling the validation server-side (i.e. ensure that the user has a proper / valid combination selected when submitting the form, if not, display validation errors).