本文介绍了在一个视图中使用多个模型-ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我需要在一个视图中进行登录,在ASP.NET MVC中注册页面。但是应用程序需要有2个独立的模型类用于登录和注册操作,请查看下面的代码。



查看

I need to do a login ,register page in ASP.NET MVC within one view. But application need to have 2 separate model class for login and register actions please check below code.

View

@using PAMWebApp.Models;
@model Tuple<LoginViewModel,RegisterViewModel>

  @using (Html.BeginForm("Login", "Home", FormMethod.Post, new { id = "login-form", role = "form", style = "display: block;", }))
     {
     <div class="form-group">
     @Html.TextBoxFor(m => m.Item1.UserName, new { @class = "form-control", id = "username", name = "username", type = "text", placeholder = "Username", tabIndex = 1 })
     </div>

     <div class="form-group">
      @Html.TextBoxFor(m => m.Item1.Password, new { @class = "form-control", type = "password", id = "password", name = "pass", placeholder = "Password", tabindex = 2 })
    </div>

    <div class="form-group">
     <div class="row">
       <div class="col-sm-6 col-sm-offset-3">
          <input type="submit" name="login-submit" id="login-submit" tabindex="4" class="form-control btn btn-login" value="Log In">
      </div>
       </div>
       </div>
 }



控制器


Controller

[HttpPost]
    [AllowAnonymous]
    public ActionResult Login(LoginViewModel model)
    {
        if (ModelState.IsValid)
        {
            string name = model.UserName;
            string pwd = model.Password;
        }
     }





型号



Model

public class LoginViewModel
    {
        [Required]
        [Display(Name = "User Name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }
    }

    public class RegisterViewModel
    {
        [Required]
        [Display(Name = "User Name")]
        public string UserName { get; set; }

        [Required]
        [Display(Name = "Email")]
        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; }
    }





我尝试过:





What I have tried:

It doesn't pass the values from view to controller. So please any one correct me, .It's working fine, if it is use one model

推荐答案

public class MemberModel{
    public LoginViewModel LoginModel { get; set; }
    public RegisterViewModel RegisterModel { get; set; }
    //add more properties here if necessary
}





然后你可以在你的视图中参考这样的新模型:





You can then reference the new model like this in your View:

@model PAMWebApp.Models.MemberModel 


这篇关于在一个视图中使用多个模型-ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 12:23