public ActionResult Create(int?customerSN, string petName, int?petGender, int?kindSN, int?varietySN, string petRemark) { if (customerSN == null) { ModelState.AddModelError("customerSN", "請選擇飼主"); } #region Validation if (petName == string.Empty) { ModelState.AddModelError("petName", "請輸入寵物姓名"); } if (petGender == null) { ModelState.AddModelError("petGender", "請選擇性別"); } if (kindSN == null) { ModelState.AddModelError("kindSN", "請選擇物種"); } if (varietySN == null) { ModelState.AddModelError("varietySN", "請選擇品種"); } if (!ModelState.IsValid) { List <Comm_Customer> customerList = CustomerModel.getCustomerList(); List <Comm_Kind> kindList = KindModel.getKindList(); List <Comm_Variety> varietyList = new List <Comm_Variety>(); ViewBag.customerSN = new SelectList(customerList, "SN", "customerName"); ViewBag.kindSN = new SelectList(kindList, "SN", "kindName"); ViewBag.varietySN = new SelectList(varietyList, "SN", "varietyName"); return(View()); } #endregion try { Boolean gender = false; if (petGender == 1) { gender = true; } Comm_Pet pet = new Comm_Pet(); pet.customerSN = Convert.ToInt32(customerSN); pet.petName = petName; pet.petGender = gender; pet.kindSN = Convert.ToInt32(kindSN); pet.varietySN = Convert.ToInt32(varietySN); pet.petRemark = petRemark; PetModel.Create(pet); return(RedirectToAction("Index")); } catch { return(View()); } }