コード例 #1
0
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                Student std = new Student();
                std.ConfirmPassword = Convert.ToString(collection["ConfirmPassword"]);
                std.Email = Convert.ToString(collection["Email"]);
                std.FirstName = Convert.ToString(collection["FirstName"]);
                std.LastName = Convert.ToString(collection["LastName"]);
                std.Password = Convert.ToString(collection["Password"]);
                std.Username = Convert.ToString(collection["Username"]);
                String CourseId = collection["CourseId"];

                List<Course> courseList = HttpContext.Application["CourseList"] as List<Course>;
                std.Course = courseList[Convert.ToInt32(CourseId) - 1];

                var res = CreateStudent(std);

                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                ViewBag.Students = e.Message;
                return RedirectToAction("Index");
            }
        }
コード例 #2
0
        private String CreateStudent(Student student)
        {
            try
            {
                var client = new HttpClient();
                string json = JsonConvert.SerializeObject(student);
                StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
                var response = client.PostAsync("http://localhost:10070/api/students/create", content).Result;

                return response.Content.ReadAsStringAsync().Result;
            }
            catch (Exception e)
            {
                ViewBag.Students = e.Message;
                return "Index";
            }
        }