2015-09-17 26 views
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"); 
    } 
+1

究竟是什麼問題? –

+0

問題註冊頁面本身工作,但當我在主頁上呈現它不工作。它不會將帳戶添加到數據庫。 –

+0

什麼不行?是否有錯誤訊息?如果該項目沒有添加到數據庫中,那麼這是我們需要查看的代碼。 –

回答

0

說實話整個觀點是有點亂的,當你使用Html.BeginForm()你不需要另一個form內部的。您認爲應該是這樣的:

@using (Html.BeginForm("{Action}", "{Controller}", FormMethod.Post, new { @class = "login-form"})) 
{ 
    @Html.AntiForgeryToken() 

    <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" /> 
} 

您需要的動作,註冊的名稱來代替。還要用控制器的名稱替換{Controller}

一些有用的documentation

+0

我知道我仍然是新手在mvc.And感謝它的作品。 –