Пример #1
0
        public JsonResult AssignLocation()
        {
            string             json      = Request.InputStream.ReadToEnd();
            AssignLocatioModel inputData =
                JsonConvert.DeserializeObject <AssignLocatioModel>(json);

            bool   success = false;
            string error   = "";
            Omat_tietokannatEntities entities = new Omat_tietokannatEntities();

            try
            {
                // haetaan ensin paikan id-numero koodin perusteella
                int locationId = (from l in entities.AssetLocations
                                  where l.Code == inputData.LocationCode
                                  select l.Id).FirstOrDefault();

                // haetaan laitteen id-numero koodin perusteella
                int assetId = (from a in entities.Assets
                               where a.Code == inputData.AssetCode
                               select a.Id).FirstOrDefault();

                if ((locationId > 0) && (assetId > 0))
                {
                    // tallennetaan uusi rivi aikaleiman kanssa kantaan
                    AssetLocation1 newEntry = new AssetLocation1();
                    newEntry.LocationId = locationId;
                    newEntry.AssetId    = assetId;
                    newEntry.LastSeen   = DateTime.Now;

                    entities.AssetLocations1.Add(newEntry);
                    entities.SaveChanges();

                    success = true;
                }
            }
            catch (Exception ex)
            {
                error = ex.GetType().Name + ": " + ex.Message;
            }
            finally
            {
                entities.Dispose();
            }

            // palautetaan JSON-muotoinen tulos kutsujalle
            var result = new { success = success, error = error };

            return(Json(result));
        }
Пример #2
0
        public JsonResult AssignLocation()
        {
            string json = Request.InputStream.ReadToEnd();
            AssignLocationModel inputData = JsonConvert.DeserializeObject <AssignLocationModel>(json);

            bool          success  = false;
            string        error    = "";
            AssetEntities entities = new AssetEntities();

            try
            {
                int LocationId = (from l in entities.AssetLocations
                                  where l.Code == inputData.LocationCode
                                  select l.Id).FirstOrDefault();
                int AssetId = (from a in entities.Assets
                               where a.Code == inputData.AssetCode
                               select a.Id).FirstOrDefault();

                if (LocationId > 0 && AssetId > 0)
                {
                    AssetLocation1 newEntry = new AssetLocation1();
                    newEntry.AssetId    = AssetId;
                    newEntry.LocationId = LocationId;
                    newEntry.LastSeen   = DateTime.Now;
                    entities.AssetLocations1.Add(newEntry);
                    entities.SaveChanges();
                    success = true;
                }
            }
            catch (Exception ex)
            {
                error = ex.GetType().Name + ": " + ex.Message;
            }
            finally
            {
                entities.Dispose();
            }
            var result = new { success = success, error = error };

            return(Json(result));
        }