這似乎是一個相當常見的問題,我在這裏看到了stackoverflow,但沒有人似乎有我正在尋找的答案。我有兩種形式(標準用戶登錄和註冊表格),我希望在相同的觀點。它正常工作,如果一切順利,但如果他們失敗和形式重新柱2,4不良的事情發生了:如何正確地張貼到相同的視圖內單獨的2個相似的形式?
- 這兩種形式都嘗試驗證雖然只有一人填寫。
- 「用戶名」輸入的id在兩個表單上都是相同的,因此在兩個表單中都會填入輸入到其中的數據。
我不確定這是什麼,我需要了解有關驗證,使其正常工作,任何幫助將不勝感激。
下面是代碼:
@{
ViewBag.Title = "Log On";
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@{Html.RenderPartial("LogIn", (FortyEightHourPrint.Models.LogOnModel)ViewBag.LogInModel);}
@{Html.RenderPartial("Register", (FortyEightHourPrint.Models.RegisterModel)ViewBag.RegisterModel);}
和寄存器局部視圖
@using (Html.BeginForm("Register","Account")) {
@Html.ValidationSummary(true, "Account creation was unsuccessful. Please correct the errors and try again.")
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
@Html.LabelFor((m => m.UserName))
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Email)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Email)
@Html.ValidationMessageFor(m => m.Email)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.ConfirmPassword)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.ConfirmPassword)
@Html.ValidationMessageFor(m => m.ConfirmPassword)
</div>
<p>
<input type="submit" value="Register" />
</p>
</fieldset>
</div>
}
和登錄
@using (Html.BeginForm("LogIn","Account"))
{
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
}
和當然的東西,該控制器被操作的方式
public ActionResult LogOn()
{
if ((bool)(ViewBag.OkToRedirect ?? false))
return Redirect(getReturnUrl(ViewBag.ReturnUrl));
return View();
}
public PartialViewResult LogIn()
{
return PartialView();
}
//
// POST: /Account/LogIn
[HttpPost]
public ViewResult LogIn(LogOnModel model, string returnUrl)
{
ViewBag.OkToRedirect = false;
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
ViewBag.OkToRedirect = true;
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
ViewBag.LogInModel = model;
ViewBag.ReturnUrl = returnUrl;
return View("LogOn");
}
public PartialViewResult Register()
{
return PartialView();
}
//
// POST: /Account/Register
[HttpPost]
public ViewResult Register(RegisterModel model, string returnUrl)
{
ViewBag.OkToRedirect = false;
if (ModelState.IsValid)
{
// Attempt to register the user
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
ViewBag.OkToRedirect = true;
}
else
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
ViewBag.RegisterModel = model;
ViewBag.ReturnUrl = returnUrl;
return View("LogOn");
}
另外,我是mvc的新手,所以如果有人可以提出更好的方法,我全都是耳朵。
感謝這些亞歷克斯。只是遇到了一些問題,包括一次性需要什麼?和GetName似乎不存在,是另一種擴展方法? –
我剛剛在上面的代碼中加入了GetName,並且稍微修改了它以使用Rx Framework中的Disposable類。所以它現在應該工作。 –