2
首先的工作,我有這樣的註冊頁面視圖亙古不變的主頁查看
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<form role="form" action="" method="post" class="login-form">
<div class="form-group">
<div>
@Html.TextBoxFor(model => model.Name, new { @class = "form-username form-control", placeholder = "Name" })
</div>
</div>
<div class="form-group">
@Html.TextBoxFor(model => model.UserName, new { @class = "form-username form-control", placeholder = "User Name" })
</div>
<div class="form-group">
@Html.TextBoxFor(model => model.Email, new { @class = "form-username form-control", placeholder = "Email" })
</div>
<div class="form-group">
@Html.PasswordFor(model => model.Password, new { @class = "form-username form-control", placeholder = "Password" })
</div>
<input type="submit" value="Register" class="btn" />
</form>
}
一個註冊頁面,當我嘗試與該頁面註冊,我註冊了,並添加帳戶database.But我想在Main頁面使用這個註冊頁面渲染並且像那樣使用,但是當我這樣做時它不起作用。
<div class="form-bottom">
@RenderPage("~/Views/User/Register.cshtml")
</div>
並且這是控制器代碼。
public ActionResult Register()
{
return View(new RegisterModel());
}
[HttpPost]
public async Task<ActionResult> Register(RegisterModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
var siteContext = new SiteContext();
var user = new User
{
Name = model.Name,
Email = model.Email,
Password=model.Password,
UserName=model.UserName
};
await siteContext.Users.InsertOneAsync(user);
return RedirectToAction("Profile", "MainPage");
}
究竟是什麼問題? –
問題註冊頁面本身工作,但當我在主頁上呈現它不工作。它不會將帳戶添加到數據庫。 –
什麼不行?是否有錯誤訊息?如果該項目沒有添加到數據庫中,那麼這是我們需要查看的代碼。 –