如何在视图中使用asp-for

时间:2018-02-06 22:09:40

标签: c# asp.net-core-mvc

我查看了多个答案,包括部分视图文档https://docs.microsoft.com/en-us/aspnet/core/mvc/views/partialhttps://www.codeproject.com/Articles/1108855/ways-to-Bind-Multiple-Models-on-a-View-in-MVCPosting multiple forms on MVC Razor View with a Single View Model,但我无法让他们的解决方案发挥作用。

我有一个带有个人用户身份验证的.NET Core 2.0项目。我希望我的登录页面有登录表单,忘记密码表单和忘记密码确认。我创建了 Signin.chstml

@model LoginViewModel

<div class="m-login__signin">
    <div class="m-login__head">
        <h3 class="m-login__title">
            Sign In To Admin
        </h3>
    </div>
    <form class="m-login__form m-form" asp-route-returnurl="@ViewData["ReturnUrl"]" method="post">
        <div asp-validation-summary="All" class="text-danger"></div>
        <div class="form-group m-form__group form-group">
            <input asp-for="Email" class="form-control m-input" type="text" placeholder="Email" name="email" autocomplete="off" />
            <span asp-validation-for="Email" class="text-danger"></span>
        </div>
        <div class="form-group form-group m-form__group">
            <input asp-for="Password" class="form-control m-input m-login__form-input--last" placeholder="Password" />
            <span asp-validation-for="Password" class="text-danger"></span>
        </div>
        <div class="row m-login__form-sub">
            <div class="col m--align-left m-login__form-left">
                <label asp-for="RememberMe" class="m-checkbox  m-checkbox--focus">
                    <input asp-for="RememberMe" />
                    @Html.DisplayNameFor(m => m.RememberMe)
                    <span></span>
                </label>
            </div>
            <div class="col m--align-right m-login__form-right">
                <a href="javascript:;" id="m_login_forget_password" class="m-link">
                    Forget Password ?
                </a>
            </div>
        </div>
        <div class="m-login__form-action">
            <button type="submit" class="btn btn-focus m-btn m-btn--pill m-btn--custom m-btn--air m-login__btn m-login__btn--primary">Log in</button>
        </div>
    </form>
</div>

ForgotPass.cshtml

 @model ForgotPasswordViewModel

    <div class="m-login__forget-password">
        <div class="m-login__head">
            <h3 class="m-login__title">
                Forgotten Password ?
            </h3>
            <div class="m-login__desc">
                Enter your email to reset your password:
            </div>
        </div>
        <form class="m-login__form m-form" asp-action="ForgotPassword" method="post">
            <div asp-validation-summary="All" class="text-danger"></div>
            <div class="form-group m-form__group">
                <input asp-for="Email" class="form-control m-input" type="text" placeholder="Email" name="email" id="m_email" autocomplete="off">
                <span asp-validation-for="Email" class="text-danger"></span>
            </div>
            <div class="m-login__form-action">
                <button type="submit" class="btn btn-focus m-btn m-btn--pill m-btn--custom m-btn--air  m-login__btn m-login__btn--primaryr">
                    Request
                </button>
                &nbsp;&nbsp;
                <button id="m_login_forget_password_cancel" class="btn btn-outline m-btn m-btn--pill m-btn--custom m-login__btn">
                    Cancel
                </button>
            </div>
        </form>
    </div>

在Shared文件夹下。

我在模型

下有一个组合视图模型
 public class LoginForgotPasswordComboViewModel
    {
        public LoginViewModel LoginViewModel { get; set; }
        public ForgotPasswordViewModel ForgotPasswordViewModel { get; set; }
    }

我的 Login.cshtml

@using System.Collections.Generic
@using System.Linq
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Http.Authentication
@model LoginForgotPasswordComboViewModel
@inject SignInManager<ApplicationUser> SignInManager


@Html.Partial("../Shared/SignIn.cshtml")
 @Html.Partial("ForgotPass")

和Controller AccountController.cs

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
    ViewData["ReturnUrl"] = returnUrl;               
    if (ModelState.IsValid)
    {                   
        var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
        if (result.Succeeded)
        {
             return RedirectToLocal(returnUrl);
        }
        else
        {
            ModelState.AddModelError(string.Empty, "Invalid login attempt.");                    
            return View(model);
        }
    }

    return View(model);
}         

视图在页面上呈现。我可以使用正确的用户名和用户名登录PW。我可以成功请求密码重置。我的问题是这个

1)如果用户名/密码不正确,我收到错误

  

InvalidOperationException:传递给ViewDataDictionary的模型项是&gt;类型&#39; TestProject.Models.AccountViewModels.LoginViewModel&#39;,但是这个&gt; ViewDataDictionary实例需要一个类型为&#39; TestProject的模型项。 Models.AccountViewModels.LoginForgotPasswordComboViewModel&#39;

我尝试将以下内容添加到控制器以返回LoginForgotPasswordComboViewModel

var return_model = new LoginForgotPasswordComboViewModel { LoginViewModel = model };
return View(return_model);

导致错误

  

InvalidOperationException:传递给ViewDataDictionary的模型项的类型为&#39; TestProject.Models.AccountViewModels.LoginForgotPasswordComboViewModel&#39;,但此ViewDataDictionary实例需要类型为&#39; TestProject.Models.AccountViewModels的模型项。 LoginViewModel&#39;

2)在我请求密码重置后,它会将我重定向到ForgotPassword页面而不是Login页面 - 默认情况下这会被控制在哪里?

1 个答案:

答案 0 :(得分:0)

当我改变时它起作用

return View(model);

return View("~/Views/Account/Login.cshtml");

没有在

中传递模型