如何通过 asp.net 核心的另一个视图从一个视图获取内容?
How to get content from a view throught another view with asp.net core?
我正在尝试将身份登录的视图获取到另一个视图上的弹出窗口中。我该怎么做?这是我目前所拥有的:
<div id="logoContainer">
<button id="loginBtn">LogIn</button>
</div>
<div id="myModal" class="modal">
<div class="modal-content animated fadeIn">
<span class="close" onclick="closeModal()">×</span>
<div id="login" class="added">
<!-- Here is where the login view should appear -->
</div>
</div>
</div>
<script>
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("loginBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function () {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function () {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
if(event.target == modal) {
modal.style.display = "none";
}
}
</script>
注意:我使用的是 ASP.NET 核心版本 3.1,因此访问身份 类 有点不同。
I’m trying to get the view from the identity login into a popup that is on another view. How shoul I do that?
您可以参考下面的示例代码来实现您的需求。
创建一个名为 _IdentityLoginPartial
的局部视图,内容如下
@model IdentityLoginModel
<div class="row">
<div class="col-md-4">
<section>
<form id="account" method="post" asp-area="Identity" asp-page="/Account/Login">
<h4>Use a local account to log in.</h4>
<hr />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="Input.Email"></label>
<input asp-for="Input.Email" class="form-control" />
<span asp-validation-for="Input.Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Input.Password"></label>
<input asp-for="Input.Password" class="form-control" />
<span asp-validation-for="Input.Password" class="text-danger"></span>
</div>
<div class="form-group">
<div class="checkbox">
<label asp-for="Input.RememberMe">
<input asp-for="Input.RememberMe" />
@Html.DisplayNameFor(m => m.Input.RememberMe)
</label>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Log in</button>
</div>
<div class="form-group">
<p>
<a id="forgot-password" asp-area="Identity" asp-page="/Account/ForgotPassword">Forgot your password?</a>
</p>
<p>
<a asp-area="Identity" asp-page="/Account/Register" asp-route-returnUrl="@Model.ReturnUrl">Register as a new user</a>
</p>
</div>
</form>
</section>
</div>
<div class="col-md-6 col-md-offset-2">
<section>
<h4>Use another service to log in.</h4>
<hr />
@{
if ((Model.ExternalLogins?.Count ?? 0) == 0)
{
<div>
<p>
There are no external authentication services configured. See <a href="https://go.microsoft.com/fwlink/?LinkID=532715">this article</a>
for details on setting up this ASP.NET application to support logging in via external services.
</p>
</div>
}
else
{
<form id="external-account" asp-page="./ExternalLogin" asp-route-returnUrl="@Model.ReturnUrl" method="post" class="form-horizontal">
<div>
<p>
@foreach (var provider in Model.ExternalLogins)
{
<button type="submit" class="btn btn-primary" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button>
}
</p>
</div>
</form>
}
}
</section>
</div>
</div>
@section Scripts {
<partial name="_ValidationScriptsPartial" />
}
自定义classIdentityLoginModel
public class IdentityLoginModel
{
public InputModel Input { get; set; }
public IList<AuthenticationScheme> ExternalLogins { get; set; }
public string ReturnUrl { get; set; }
public class InputModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
}
参考_IdentityLoginPartial
部分视图并将其作为模态弹出窗口的一部分
<div id="myModal" class="modal">
<div class="modal-content animated fadeIn">
<span class="close" onclick="closeModal()">×</span>
<div id="login" class="added">
<partial name="_IdentityLoginPartial" model='new IdentityLoginModel { ReturnUrl = "/" }' />
</div>
</div>
</div>
测试结果
我正在尝试将身份登录的视图获取到另一个视图上的弹出窗口中。我该怎么做?这是我目前所拥有的:
<div id="logoContainer">
<button id="loginBtn">LogIn</button>
</div>
<div id="myModal" class="modal">
<div class="modal-content animated fadeIn">
<span class="close" onclick="closeModal()">×</span>
<div id="login" class="added">
<!-- Here is where the login view should appear -->
</div>
</div>
</div>
<script>
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("loginBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function () {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function () {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function (event) {
if(event.target == modal) {
modal.style.display = "none";
}
}
</script>
注意:我使用的是 ASP.NET 核心版本 3.1,因此访问身份 类 有点不同。
I’m trying to get the view from the identity login into a popup that is on another view. How shoul I do that?
您可以参考下面的示例代码来实现您的需求。
创建一个名为 _IdentityLoginPartial
的局部视图,内容如下
@model IdentityLoginModel
<div class="row">
<div class="col-md-4">
<section>
<form id="account" method="post" asp-area="Identity" asp-page="/Account/Login">
<h4>Use a local account to log in.</h4>
<hr />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="Input.Email"></label>
<input asp-for="Input.Email" class="form-control" />
<span asp-validation-for="Input.Email" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Input.Password"></label>
<input asp-for="Input.Password" class="form-control" />
<span asp-validation-for="Input.Password" class="text-danger"></span>
</div>
<div class="form-group">
<div class="checkbox">
<label asp-for="Input.RememberMe">
<input asp-for="Input.RememberMe" />
@Html.DisplayNameFor(m => m.Input.RememberMe)
</label>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Log in</button>
</div>
<div class="form-group">
<p>
<a id="forgot-password" asp-area="Identity" asp-page="/Account/ForgotPassword">Forgot your password?</a>
</p>
<p>
<a asp-area="Identity" asp-page="/Account/Register" asp-route-returnUrl="@Model.ReturnUrl">Register as a new user</a>
</p>
</div>
</form>
</section>
</div>
<div class="col-md-6 col-md-offset-2">
<section>
<h4>Use another service to log in.</h4>
<hr />
@{
if ((Model.ExternalLogins?.Count ?? 0) == 0)
{
<div>
<p>
There are no external authentication services configured. See <a href="https://go.microsoft.com/fwlink/?LinkID=532715">this article</a>
for details on setting up this ASP.NET application to support logging in via external services.
</p>
</div>
}
else
{
<form id="external-account" asp-page="./ExternalLogin" asp-route-returnUrl="@Model.ReturnUrl" method="post" class="form-horizontal">
<div>
<p>
@foreach (var provider in Model.ExternalLogins)
{
<button type="submit" class="btn btn-primary" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button>
}
</p>
</div>
</form>
}
}
</section>
</div>
</div>
@section Scripts {
<partial name="_ValidationScriptsPartial" />
}
自定义classIdentityLoginModel
public class IdentityLoginModel
{
public InputModel Input { get; set; }
public IList<AuthenticationScheme> ExternalLogins { get; set; }
public string ReturnUrl { get; set; }
public class InputModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
}
参考_IdentityLoginPartial
部分视图并将其作为模态弹出窗口的一部分
<div id="myModal" class="modal">
<div class="modal-content animated fadeIn">
<span class="close" onclick="closeModal()">×</span>
<div id="login" class="added">
<partial name="_IdentityLoginPartial" model='new IdentityLoginModel { ReturnUrl = "/" }' />
</div>
</div>
</div>
测试结果