title: Ajax Not Working on Edit Method
body: <p>I have an ajax method for a cascading drop down list, similar to country - state drop down list.  It works fine on Create method, but I can't get it to work on Edit method.  I get this error on Edit:</p>
  
  <pre><code>POST http://localhost/Submissions/Edit/SomeAJAXMethod 500 (Internal Server Error)
  </code></pre>
  
  <p>There is an extra /Edit in the path that makes it not work.  It works just fine on Create.  I tried to add ~/.  I get these errors on Create and Edit method:</p>
  
  <pre><code>POST http://localhost/Submissions/Edit/~/SomeAJAXMethod 404 (Not Found)
  </code></pre>
  
  <p>I tried to add ../.  I get this error on Create, works fine on Edit:</p>
  
  <pre><code>POST http://localhost/SomeAJAXMethod 404 (Not Found)
  </code></pre>
  
  <p>How do I make it work on both Create and Edit? </p>
  
  <p>Below is my controller.<br>
  //
      // GET: /Submissions/Create</p>
  
  <pre><code>public ActionResult Create()
  {
      ViewBag.ActivityRejectCodeId = GetRejectCodesByActivity(0);
      ViewBag.Activities = GetActivities();
      ViewBag.Workstations = GetWorkstationsByActivity(0);
      ViewBag.Platforms = GetPlatformsByWorkstation(0, 0);
      ViewBag.Parts = GetPartsByPlatform(0, 0, 0);
      ViewBag.StatusId = new SelectList(db.Status, "Id", "Name");
      ViewBag.Technicians = GetTechnicians(0);
      ViewBag.Shifts = GetShiftsByTechnician(0, 0);
      ViewBag.WorkOrderId = new SelectList(db.WorkOrders, "Id", "WO");
  
      return View();
  }
  
  //
  // POST: /Submissions/Create
  
  [HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Create(WorkOrderSubmission workordersubmission)
  {
      var activityId = Int32.Parse(Request.Form["ActivityId"]);
      var workstationId = Int32.Parse(Request.Form["WorkstationId"]);
      var platformId = Int32.Parse(Request.Form["PlatformId"]);
      var partId = Int32.Parse(Request.Form["PartId"]);
      var techId = Int32.Parse(Request.Form["TechnicianId"]);
      var shiftId = Int32.Parse(Request.Form["ShiftId"]);
  
      if (ModelState.IsValid)
      {
          // Get the PlatformStageId from the combination of (ActivityId, WorkstationId, PlatformId, PartId)
          var rs = (from ps in db.PlatformStages
                    join wa in db.WorkstationActivities on ps.WorkstationActivityId equals wa.Id
                    join pp in db.PlatformParts on ps.PlatformPartId equals pp.Id
                    where ps.WorkstationActivity.ActivityId == activityId
                      &amp;&amp; ps.WorkstationActivity.WorkstationId == workstationId
                      &amp;&amp; ps.PlatformPart.PlatformId == platformId
                      &amp;&amp; ps.PlatformPart.PartId == partId
                    select new {
                        ps.Id
                    }).FirstOrDefault();
          workordersubmission.PlatformStageId = rs.Id;
  
          // Get TechnicianShiftId from the combination of (TechnicianId, ShiftId)
          rs = (from ts in db.TechnicianShifts
                where ts.TechnicianId == techId
                  &amp;&amp; ts.ShiftId == shiftId
                select new
                {
                    ts.Id
                }).FirstOrDefault();
          workordersubmission.TechnicianShiftId = rs.Id;
  
          workordersubmission.SubmissionDate = DateTime.Now;
  
          db.WorkOrderSubmissions.Add(workordersubmission);
          db.SaveChanges();
  
          return RedirectToAction("Index");
      }
  
      ViewBag.ActivityRejectCodeId = new SelectList(db.ActivityRejectCodes, "Id", "RejectCodeId", workordersubmission.ActivityRejectCodeId);
      ViewBag.Activities = GetActivities(activityId);
      ViewBag.Workstations = GetWorkstationsByActivity(activityId, workstationId);
      ViewBag.Platforms = GetPlatformsByWorkstation(activityId, workstationId, platformId);
      ViewBag.Parts = GetPartsByPlatform(activityId, workstationId, platformId, partId);
      ViewBag.StatusId = new SelectList(db.Status, "Id", "Name", workordersubmission.StatusId);
      ViewBag.Technicians = GetTechnicians(techId);
      ViewBag.Shifts = GetShiftsByTechnician(techId, shiftId);
      ViewBag.WorkOrderId = new SelectList(db.WorkOrders, "Id", "WO", workordersubmission.WorkOrderId);
  
      return View(workordersubmission);
  }
  
  //
  // GET: /Submissions/Edit/5
  
  public ActionResult Edit(int id = 0)
  {
      WorkOrderSubmission workordersubmission = db.WorkOrderSubmissions.Find(id);
  
      if (workordersubmission == null)
      {
          return HttpNotFound();
      }
  
      var platformStageId = workordersubmission.PlatformStageId;
      var technicianShiftId = workordersubmission.TechnicianShiftId;
  
      // Get ActivityId, WorkstationId, PlatformId, PartId from PlatformStageId
      var rs = (from ps in db.PlatformStages
                join wa in db.WorkstationActivities on ps.WorkstationActivityId equals wa.Id
                join pp in db.PlatformParts on ps.PlatformPartId equals pp.Id
                where ps.Id == platformStageId
                select new
                {
                    ActivityId = wa.ActivityId,
                    WorkstationId = wa.WorkstationId,
                    PlatformId = pp.PlatformId,
                    PartId = pp.PartId
                })
                .FirstOrDefault();
  
      ViewBag.Activities = GetActivities(rs.ActivityId);
      ViewBag.Workstations = GetWorkstationsByActivity(rs.ActivityId, rs.WorkstationId);
      ViewBag.Platforms = GetPlatformsByWorkstation(rs.ActivityId, rs.WorkstationId, rs.PlatformId);
      ViewBag.Parts = GetPartsByPlatform(rs.ActivityId, rs.WorkstationId, rs.PlatformId, rs.PartId);
  
      // Get TechnicianId, ShiftId from TechnicianShiftId
      var rs2 = (from ts in db.TechnicianShifts
            where ts.Id == technicianShiftId
            select new //TechnicianShift
            {
                TechnicianId = ts.TechnicianId,
                ShiftId = ts.ShiftId
            })
            .FirstOrDefault();
  
      ViewBag.Technicians = GetTechnicians(rs2.TechnicianId);
      ViewBag.Shifts = GetShiftsByTechnician(rs2.TechnicianId, rs2.ShiftId);
  
      ViewBag.ActivityRejectCodeId = new SelectList(db.ActivityRejectCodes, "Id", "RejectCodeId", workordersubmission.ActivityRejectCodeId);
      ViewBag.StatusId = new SelectList(db.Status, "Id", "Name", workordersubmission.StatusId);
      ViewBag.WorkOrderId = new SelectList(db.WorkOrders, "Id", "WO", workordersubmission.WorkOrderId);
  
      return View(workordersubmission);
  }
  
  //
  // POST: /WorkorderSubmissions/Edit/5
  
  [HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Edit(WorkOrderSubmission workordersubmission)
  {
      if (ModelState.IsValid)
      {
          db.Entry(workordersubmission).State = EntityState.Modified;
          db.SaveChanges();
          return RedirectToAction("Index");
      }
  
      ViewBag.ActivityRejectCodeId = new SelectList(db.ActivityRejectCodes, "Id", "RejectCodeId", workordersubmission.ActivityRejectCodeId);
      ViewBag.PlatformStageId = new SelectList(db.PlatformStages.OrderBy(p =&gt; p.Name), "Id", "Name", workordersubmission.PlatformStageId);
      ViewBag.StatusId = new SelectList(db.Status, "Id", "Name", workordersubmission.StatusId);
      ViewBag.TechnicianShiftId = new SelectList(db.TechnicianShifts, "Id", "Description", workordersubmission.TechnicianShiftId);
      ViewBag.WorkOrderId = new SelectList(db.WorkOrders, "Id", "WO", workordersubmission.WorkOrderId);
  
      return View(workordersubmission);
  }
  </code></pre>
  
  <p>Below is the AJAX call:</p>
  
  <pre><code>$(function () {
      // Code that triggers when there is a change in Activity drop down.
      $('#ActivityId').change(function () {
          var activityId = $(this).val();
          var url = 'GetRejectCodesByActivityJson';
  
          // Empty the Reject Code drop down list.
          $('#ActivityRejectCodeId').empty();
  
          // Empty the Workstation, Platform, Part drop downs.
          $('#WorkstationId').empty();
          $('#PlatformId').empty();
          $('#PartId').empty();
  
          $('.showhide-workstation').show();
  
          // AJAX call that re-populate Reject Code drop down depending on the Activity selected.
          $.ajax({
              type: 'POST',
              url: url,
              dataType: 'json',
              data: { activityId: activityId },
              success: function (codes) {
                  $('#ActivityRejectCodeId').append('&lt;option value=""&gt;&lt;/option&gt;');
                  $.each(codes, function (i) {
                      $('#ActivityRejectCodeId').append('&lt;option value = "' + codes[i].Value + '"&gt;' + codes[i].Text + '&lt;/option&gt;')
                  });
              },
              error: function (ex) {
                  $('#ActivityRejectCodeId').append('&lt;option value=""&gt;&lt;/option&gt;');
              }
          }); // END $.ajax() on GetRejectCodesByActivityJson
  
          url = 'GetWorkstationsByActivityJson';
          // AJAX call that re-populate Workstation drop down depending on the Activity selected.
          $.ajax({
              type: 'POST',
              url: url,
              dataType: 'json',
              data: { activityId: activityId },
              success: function (codes) {
                  $('#WorkstationId').append('&lt;option value=""&gt;&lt;/option&gt;');
                  $.each(codes, function (i) {
                      $('#WorkstationId').append('&lt;option value = "' + codes[i].Value + '"&gt;' + codes[i].Text + '&lt;/option&gt;');
                  });
              },
              error: function (ex) {
                  $('#WorkstationId').append('&lt;option value=""&gt;&lt;/option&gt;');
              }
          }); // END $.ajax() on GetRejectCodesByActivityJson
      }); // END $('#ActivityId').change()
  
      // Code that triggers when there is a change in Workstation drop down.
      $('#WorkstationId').change(function () {
          var activityId = $('#ActivityId').val();
          var workstationId = $(this).val();
          var url = 'GetPlatformsByWorkstationJson';
  
          // Empty the Platform, Part drop downs.
          $('#PlatformId').empty();
          $('#PartId').empty();
  
          $('.showhide-platform').show();
  
          // AJAX call that re-populate Platform drop down depending on the Workstation selected.
          $.ajax({
              type: 'POST',
              url: url,
              dataType: 'json',
              data: { activityId: activityId, workstationId: workstationId },
              success: function (codes) {
                  $('#PlatformId').append('&lt;option value=""&gt;&lt;/option&gt;');
                  $.each(codes, function (i) {
                      $('#PlatformId').append('&lt;option value = "' + codes[i].Value + '"&gt;' + codes[i].Text + '&lt;/option&gt;');
                  });
              },
              error: function (ex) {
                  $('#PlatformId').append('&lt;option value=""&gt;&lt;/option&gt;');
              }
          }); // END $.ajax() on GetPlatformsByWorkstationJson
      }); // END $('#WorkstationId').change()
  </code></pre>