public ActionResult Create(Poll poll, string[] item) { //var ab = this.Request.Form[test0]; ViewBag.AuthorUserName = User.Identity.Name; if (poll.OpenDate != null && poll.CloseDate != null && poll.OpenDate.CompareTo(poll.CloseDate) > 0) { ModelState.AddModelError("Date","投票结束时间不能早于投票开始时间,请检查后再提交"); } if (item == null || item.Length < 2) { ModelState.AddModelError("PollItemNumber", "请至少设置两个选项"); } if (item != null) { if (poll.Items != null) { poll.Items.Clear(); } for (int i = 0; i < item.Length; i++) { if (String.IsNullOrWhiteSpace(item[i])) { ModelState.AddModelError("PollItem" + i, "请输入内容"); item[i] = " "; } PollItem tempItem = new PollItem() { Id = i, Caption = item[i], Count = 0, Poll = poll }; unitOfWork.PollItems.Add(tempItem); poll.Items.Add(tempItem); } } poll.AuthorUserName = User.Identity.Name; if (ModelState.IsValid) { unitOfWork.Polls.Add(poll); unitOfWork.SaveChanges(); return RedirectToAction("Index"); } //return Json(false); return View(poll); }
public ActionResult Edit(Poll poll) { if (ModelState.IsValid) { // db.Entry(poll).State = EntityState.Modified; // db.SaveChanges(); unitOfWork.Polls.Update(poll); unitOfWork.SaveChanges(); return RedirectToAction("Index"); } ViewBag.AuthorUserName = new SelectList(unitOfWork.Users.ToList(), "UserName", "Password", poll.AuthorUserName); return View(poll); }