title: Changing void method to ActionResult method when there is an Ajax function calling the method
body: <p>I have recently converted my method from void to an ActionResult so that I can return error messages if a condition isn't met</p>
  
  <p>here is the method </p>
  
  <pre><code>        [HttpPost]
          [ValidateAntiForgeryToken]
          [Audit]
          public ActionResult AddUnits(int so_id, int site_id, int[] addItem_id, int[] addItem_qty, int[] addItem_disc)
          {
              bool canCreate = true;
              string errors = "";
  
              // Loop however many times is necessary to iterate through the largest array
              for (int i = 0; i &lt; Math.Max(Math.Max(addItem_id.Length); i++)
              {
                  foreach (SODetails sod in db.SalesOrders.Find(so_id).SalesDetails)
                  {
                      if (i &lt; addItem_id.Length &amp;&amp; addItem_qty[i] != 0 &amp;&amp; sod.ItemID == addItem_id[i] &amp;&amp; addItem_id[i] != 365 &amp;&amp; addItem_id[i] != 410) 
                      {
                          sod.item_qty += addItem_qty[i];
                          sod.item_discount = addItem_disc[i];
                          addItem_id[i] = 0;
                          addItem_qty[i] = 0;
                          addItem_disc[i] = 0;
                      }
                  }
                  db.SaveChanges();
  
                  if(i &lt; addItem_qty.Length &amp;&amp; addItem_qty[i] != 0)
                  {
                      SODetails sODetails = new SODetails
                      {
                          SalesOrderID = so_id,
                          SiteID = site_id
                      };
  
                      // Only add a unit to the SODetails object if it's not null and has an id and quanitity specified
                      if(i &lt; addItem_id.Length &amp;&amp; addItem_id[i] != 0 &amp;&amp; addItem_qty[i] != 0)
                      {
                          sODetails.ItemID = addItem_id[i];
                          sODetails.item_qty = addItem_qty[i];
                          sODetails.item_discount = addItem_disc[i];
                      }
  
                      if (sODetails.SiteID == 0)
                          sODetails.SiteID = null;
  
                      // Here on is basically the same as the old post function below
                      SalesOrder SO = db.SalesOrders.Find(sODetails.SalesOrderID);
                      SODetails salesOrderDetails = db.SODetails.Add(sODetails);
                      salesOrderDetails.SalesOrder = SO;
  
                      Item SO_Item = db.Items.Find(sODetails.ItemID);
  
                      if (SO_Item != null)
                      {
  
                          if (SO.OrderType == SOType.OffSiteInventory &amp;&amp; salesOrderDetails.siteAvailable(SO_Item) &lt; salesOrderDetails.item_qty)
                          {
                              errors += "Not enough" + SO_Item.Name + "in inventory location";
                              canCreate = false;                           
                          }
                          else { 
  
                          sODetails.item_qty = sODetails.item_qty == null ? 0 : sODetails.item_qty;
                          int qtyOrdered = sODetails.item_qty == null ? 0 : (int)sODetails.item_qty;
                          salesOrderDetails.dynamicItem_qty = qtyOrdered;
  
                          if (SO_Item.SalesOrderMessage != null)
                              TempData["SalesOrderMessage"] = SO_Item.SalesOrderMessage;
  
                          }
                      }
  
                      if (!canCreate)
                      {
                          ModelState.AddModelError(string.Empty, errors);
                          //return View(so_id);
                          return RedirectToAction("Details", "SODetails", new { orderId = so_id });
                      }
  
                      db.SODetails.Add(sODetails);                   
                  }
              }
  
              db.SaveChanges();
  
              return RedirectToAction("Details", "SODetails", new { orderId = so_id });
          }
  </code></pre>
  
  <p>The method is called through my Ajax function that looks like this</p>
  
  <pre><code>  $.ajax({
              type: "POST",
              url: "@IGT.baseUrl/SODetailsAjax/AddUnits",
              traditional: true,
              data: {
                  __RequestVerificationToken: token, 
                  so_id: @Int32.Parse(Request["orderId"]),
                  site_id: site,
                  addItem_id: items,
                  addItem_qty: itemsqty,
                  addItem_disc: itemsdisc,
                  addComp_id: comps,
                  addComp_qty: compsqty,
                  addComp_disc: compsdisc,
                  addPart_id: parts,
                  addPart_qty: partsqty,
                  addPart_disc: partsdisc
              },
              success: function () {
                  location.href = "../SalesOrders/Details?id=@so.ID";
              },
              error: function (jqXHR, status, error) {
                  alert("Error: " + error);
              }
          });
  </code></pre>
  
  <p>Each time the method is run now it returns error instead of success. How can I change my code so it still returns success if the conditions were valid?</p>