Beispiel #1
0
        public ActionResult Create(DogsCreateRequest model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Create", model));
            }

            if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0)
            {
                string fileName  = Path.GetFileNameWithoutExtension(model.ImageUpload.FileName);
                string extension = Path.GetExtension(model.ImageUpload.FileName);
                fileName    = fileName + extension;
                model.Image = "~/Image/" + fileName;
                fileName    = Path.Combine(Server.MapPath("~/Image/"), fileName);
                model.ImageUpload.SaveAs(fileName);
            }

            if (model.ImageUpload == null || model.ImageUpload.ContentLength == 0)
            {
                model.Image = "~/Image/image-not-found.png";
            }


            DogService ds = new DogService();

            ds.Create(model);
            return(RedirectToAction("DogsIndex"));
        }
Beispiel #2
0
        //Inserts a record into the database and returns an Id
        public int Create(DogsCreateRequest model)
        {
            int Id = 0;

            using (SqlConnection conn = new SqlConnection(_connection))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection  = conn;
                cmd.CommandText = "spFamilyDogs_Insert";
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@Name", model.Name);
                cmd.Parameters.AddWithValue("@Breed", model.Breed);
                cmd.Parameters.AddWithValue("@Color", model.Color ?? (object)DBNull.Value);
                cmd.Parameters.AddWithValue("@Size", model.Size);
                cmd.Parameters.AddWithValue("@LivingArea", model.LivingArea);
                cmd.Parameters.AddWithValue("@LifeExpt", model.LifeExpectancy);
                cmd.Parameters.AddWithValue("@ShedScore", model.ShedScore);
                cmd.Parameters.AddWithValue("@AggressiveScore", model.AgressiveScore);
                cmd.Parameters.AddWithValue("@ExerciseScore", model.ExerciseScore);
                cmd.Parameters.AddWithValue("@ImageUrl", model.Image ?? (object)DBNull.Value);


                SqlParameter outParam = new SqlParameter("@Id", SqlDbType.Int);
                outParam.Direction = ParameterDirection.Output;
                cmd.Parameters.Add(outParam);

                conn.Open();
                cmd.ExecuteNonQuery();

                Id = Convert.ToInt32(cmd.Parameters["@Id"].Value);

                return(Id);
            }
        }
        public HttpResponseMessage Create(DogsCreateRequest model)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            ItemResponse <int> response = new ItemResponse <int>();

            response.Item = ds.Create(model);
            return(Request.CreateResponse(HttpStatusCode.OK, response));
        }