Ejemplo n.º 1
0
        public ActionResult Create(OwnerEditViewmodel owner)
        {
            try
            {
                using (SqlConnection conn = Connection)
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = @"INSERT INTO Owner (Name, Address, Phone, NeighborhoodId)
                                            OUTPUT INSERTED.Id
                                            VALUES (@name, @address, @phone, @neighborhoodId)";

                        cmd.Parameters.Add(new SqlParameter("@name", owner.Name));
                        cmd.Parameters.Add(new SqlParameter("@address", owner.Address));
                        cmd.Parameters.Add(new SqlParameter("@phone", owner.Phone));
                        cmd.Parameters.Add(new SqlParameter("@neighborhoodId", owner.NeighborhoodId));

                        var id = (int)cmd.ExecuteScalar();
                        owner.Id = id;
                        // TODO: Add insert logic here

                        return(RedirectToAction(nameof(Index)));
                    }
                }
            }
            catch
            {
                return(View());
            }
        }
Ejemplo n.º 2
0
        // GET: Owners/Create
        public ActionResult Create()
        {
            var neighborhoodOptions = GetNeighborhoodOptions();
            var viewModel           = new OwnerEditViewmodel()
            {
                NeighborhoodOptions = neighborhoodOptions
            };

            return(View(viewModel));
        }
Ejemplo n.º 3
0
        // GET: Owners/Edit/1
        public ActionResult Edit(int id)
        {
            var owner = GetOwnerById(id);
            var neighborhoodOptions = GetNeighborhoodOptions();
            var viewModel           = new OwnerEditViewmodel()
            {
                Id                  = owner.Id,
                Name                = owner.Name,
                Address             = owner.Address,
                NeighborhoodId      = owner.NeighborhoodId,
                Phone               = owner.Phone,
                NeighborhoodOptions = neighborhoodOptions
            };

            return(View(viewModel));
        }
Ejemplo n.º 4
0
        public ActionResult Edit(int id, OwnerEditViewmodel owner)
        {
            try
            {
                using (SqlConnection conn = Connection)
                {
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = @"UPDATE Owner
                                            SET Name = @name, 
                                                Address = @address, 
                                                Phone = @phone, 
                                                NeighborhoodId = @neighborhoodId
                                            WHERE Id = @id";

                        cmd.Parameters.Add(new SqlParameter("@name", owner.Name));
                        cmd.Parameters.Add(new SqlParameter("@address", owner.Address));
                        cmd.Parameters.Add(new SqlParameter("@phone", owner.Phone));
                        cmd.Parameters.Add(new SqlParameter("@neighborhoodId", owner.NeighborhoodId));
                        cmd.Parameters.Add(new SqlParameter("@id", id));

                        var rowsAffected = cmd.ExecuteNonQuery();

                        if (rowsAffected < 1)
                        {
                            return(NotFound());
                        }
                    }
                }

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(View());
            }
        }