提問者
資料驗證失敗後,會無限導到首頁

問題
-
前輩好:
目前我在做自定義資料驗證的部分,
但若驗證失敗時會導回首頁,然後在首頁不斷的驗證失敗..
追了程式碼猜測:
我驗證失敗時,回傳一個View,
然後這個View,有套用主版頁面,主版頁面其中一段程式碼為:
@if (User.Identity.IsAuthenticated) { @Html.Action("Login", "Account") }
回到Login/Account後,一直執行裡面的這段,就跑到掛了.
if (!ModelState.IsValid) { return View(); }
如果是我理解錯誤還請跟我說。
附上相關原始碼:
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(UserCreateViewModels UserTableCreate, string[] Categories) { if (ModelState.IsValid) { Account account = (Account)Session["account"]; UserService userService = new UserService(); int power = 0; for (int i = 0; i < Categories.Length; i++) { power = power + Convert.ToInt32(Categories[i]); } userService.Create(UserTableCreate, power, account.UserID); return RedirectToAction("Index"); } List<SelectListItem> mySelectItemList = new List<SelectListItem>(); SetUnitDropDownList(ref mySelectItemList); mySelectItemList[Convert.ToInt32(UserTableCreate.UnitName)].Selected = true; UserTableCreate.DDLUserDepart = mySelectItemList; var items = this.CategorySelectListItems(); ViewBag.CategoryItems = items; return View(UserTableCreate); }
想請問該怎麼解決這問題....
希望前輩能給點方向,謝謝
所有回覆
-
如果您是要驗証身份的功能, 可以直接使用[Authorize], 請參考:
ASP.NET MVC 4 AllowAnonymous Attribute and Authorize Attribute
-
前輩我不是要驗證身份,
而是我自己定義的資料驗證失敗後(例如帳號重複),會不斷重複導回Login頁,
我想是我Action中的Create驗證失敗後,回傳View(ViewModel);
if (!ModelState.IsValid) { return View(UserTableViewModels); }
而那個View有套Layout,
Layout有一段是
if (!ModelState.IsValid) { return View(); }
而ModelState.IsValid一直是False導致的。
但這只是我的猜測..
然後我也還在想該怎麼解..
-
話說為啥 Layout 會有需要那段代碼呢?
@if (User.Identity.IsAuthenticated) { @Html.Action("Login", "Account") }
你說這段放在 Layout 那其他的頁面載入的時候 也會發生錯誤嗎?- 已編輯 Coding Kid Peter Chang 2018年11月13日 上午 08:11
-
To Coding Kid前輩:
其他頁面載入時不會發生錯誤,
這邊是想要動態產生功能權限,
登入後依據不同的角色有不同的功能列表,
若正確回傳的是一個partialView
輸入密碼後,正確時都是跑下面這個。
[AllowAnonymous] public ActionResult Login() { Account account = null; if (Session["account"] != null) { account = (Account)Session["account"]; return PartialView("_Account", account); } return View(); }
輸入密碼時跟在別頁面資料驗證失敗時,是跑下面
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public ActionResult Login(FormCollection form, LoginViewModels model, string returnUrl) { if (!ModelState.IsValid) { return View(); } Account account = null; if (Session["account"] == null || model.Account == "Admin") { account = new Account(); PageTableRepository pageTableRepository = new PageTableRepository(); var pageInfo = pageTableRepository.GetPriv(1); var grouped = from row in pageInfo group row by row.PageCatagory into g select new { GroupKey = g.Key }; foreach (var item in grouped) { account.PageGroupName.Add(item.GroupKey); }
//先給1,用Admin登入,開發完後改 account.Page_Info = pageInfo; account.RoleID = 1; account.UserID = 1; account.UnitID = 1; account.UserName = "Admin"; logger.Info(account.UserName + "登入系統"); Session["account"] = account; var ticket = new FormsAuthenticationTicket(version: 1, name: account.UserName, issueDate: DateTime.Now, expiration: DateTime.Now.AddMinutes(300), isPersistent: false, userData: account.UserName, cookiePath: FormsAuthentication.FormsCookiePath); var encryptedTicket = FormsAuthentication.Encrypt(ticket); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); Response.Cookies.Add(cookie); } else { account = (Account)Session["account"]; return View(); } return RedirectToAction("Index", "Home"); }
是我寫法太不正常嗎?
若我寫的架構是錯誤的,我可以改掉,謝謝前輩