0
在一個經典的ASP.Net
MVC
項目我想將註冊鏈接功能移動到一個按鈕。 (所以點擊按鈕時發生的註冊點擊事件就會發生)。夠簡單!我做了以下。如何從JavaScript調用默認的註冊方法?在asp.net mvc?
Login.cshtml
<div class="form-group col-md-8 col-md-push-1">
<div class="row">
<input type="submit" value="Log in" class="btn btn-primary" />
<input type="button" value="Register" class="btn btn-primary" onclick="register();"/>
</div>
</div>
添加腳本到同一頁面
<script type="text/javascript">
function register() {
$.ajax({
url: "@Url.Action("Register", "Account")",
success: function (data) {
alert('success');
},
type: 'GET'
});
}
</script>
現在賬戶控制器的註冊函數有兩個重載
public ActionResult Register()
{
return View();
}
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var geUser = new User { Password = model.Password, Username = model.Email, AccessLevel = "0003", LoggedIn = false, Active = true, AllowOffline = false, LastSettingsRefreshed = DateTime.Now };
var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, user = geUser};
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInAsync(user, isPersistent: false);
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
// string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
// var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
// await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("Index", "Home");
}
else
{
AddErrors(result);
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
小號o現在我想調用第二個函數而不是第一個函數...
- 如何從JavaScript傳遞RegisterViewModel?或
- 由於目前我打電話沒有參數的功能,我可以改變它,使它重定向到註冊頁面?
RegisterViewModel
public class RegisterViewModel
{
[Required]
[Display(Name = "Username")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
開始通過將其更改爲'type:'GET''。但是你沒有顯示'RegisterViewModel'的視圖的相關部分,所以它不可能告訴如何發送與該模型相關的數據。 –
並返回RedirectToAction(「Index」,「Home」);'在你的POST方法中沒有意義 - 它和ajax調用和ajax調用都不會重定向。你爲什麼要做一個Ajax調用而不是一個正常的提交,爲什麼你在同一個bview中同時登錄和註冊表單? –
嗨斯蒂芬。我已添加RegisterViewModel – Samra