多个模型视图中 [英] Multiple models in a view

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

问题描述

我想有2种型号在一个视图中。该页面包含 LoginViewModel RegisterViewModel

例如。

 公共类LoginViewModel
{
    公共字符串电子邮件{获得;组; }
    公共字符串密码{搞定;组; }
}公共类RegisterViewModel
{
    公共字符串名称{;组; }
    公共字符串电子邮件{获得;组; }
    公共字符串密码{搞定;组; }
}

我是否需要再拍视图模型持有这2的ViewModels?

 公共BigViewModel
{
    公共LoginViewModel LoginViewModel {搞定;组;}
    公共RegisterViewModel RegisterViewModel {搞定;组;}
}

我需要验证属性被提前到认为,这就是为什么我需要的ViewModels。

是不是有另一种方式,如(无 BigViewModel

  @model ViewModel.RegisterViewModel
 @using(Html.BeginForm(登录,验证,FormMethod.Post))
 {
        @ Html.TextBoxFor(型号=> model.Name)
        @ Html.TextBoxFor(型号=> model.Email)
        @ Html.PasswordFor(型号=> model.Password)
 } @model ViewModel.LoginViewModel
 @using(Html.BeginForm(登录,验证,FormMethod.Post))
 {
        @ Html.TextBoxFor(型号=> model.Email)
        @ Html.PasswordFor(型号=> model.Password)
 }


解决方案

有很多方法...


  1. 与BigViewModel
    你做的:

      @model BigViewModel
    @using(Html.BeginForm()){
        @ Html.EditorFor(O => o.LoginViewModel.Email)
        ...
    }


  2. 您可以创建2个附加的意见

    Login.cshtml

      @model ViewModel.LoginViewModel
    @using(Html.BeginForm(登录,验证,FormMethod.Post))
    {
        @ Html.TextBoxFor(型号=> model.Email)
        @ Html.PasswordFor(型号=> model.Password)
    }

    和register.cshtml的同样的事情

    创建后,你必须使它们在主视图中,并通过他们的视图模型/可视数据

    所以它可能是这样的:

      @ {Html.RenderPartial(登录,ViewBag.Login);}
    @ {Html.RenderPartial(登记,ViewBag.Register);}

      @ {Html.RenderPartial(登录,Model.LoginViewModel)}
    @ {Html.RenderPartial(登记,Model.RegisterViewModel)}


  3. 使用

    您网站的AJAX部分变得更加独立


  4. 内部框架,但可能不是这种情况


I want to have 2 models in one view. The page contains both LoginViewModel and RegisterViewModel.

e.g.

public class LoginViewModel
{
    public string Email { get; set; }
    public string Password { get; set; }
}

public class RegisterViewModel
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
}

Do I need to make another ViewModel which holds these 2 ViewModels?

public BigViewModel
{
    public LoginViewModel LoginViewModel{get; set;}
    public RegisterViewModel RegisterViewModel {get; set;}
}

I need the validation attributes to be brought forward to the view, this is why I need the ViewModels.

Isn't there another way such as (without the BigViewModel):

 @model ViewModel.RegisterViewModel
 @using (Html.BeginForm("Login", "Auth", FormMethod.Post))
 {
        @Html.TextBoxFor(model => model.Name)
        @Html.TextBoxFor(model => model.Email)
        @Html.PasswordFor(model => model.Password)
 }

 @model ViewModel.LoginViewModel
 @using (Html.BeginForm("Login", "Auth", FormMethod.Post))
 {
        @Html.TextBoxFor(model => model.Email)
        @Html.PasswordFor(model => model.Password)
 }

解决方案

There are lots of ways...

  1. with your BigViewModel you do:

    @model BigViewModel    
    @using(Html.BeginForm()) {
        @Html.EditorFor(o => o.LoginViewModel.Email)
        ...
    }
    

  2. you can create 2 additional views

    Login.cshtml

    @model ViewModel.LoginViewModel
    @using (Html.BeginForm("Login", "Auth", FormMethod.Post))
    {
        @Html.TextBoxFor(model => model.Email)
        @Html.PasswordFor(model => model.Password)
    }
    

    and register.cshtml same thing

    after creation you have to render them in the main view and pass them the viewmodel/viewdata

    so it could be like this:

    @{Html.RenderPartial("login", ViewBag.Login);}
    @{Html.RenderPartial("register", ViewBag.Register);}
    

    or

    @{Html.RenderPartial("login", Model.LoginViewModel)}
    @{Html.RenderPartial("register", Model.RegisterViewModel)}
    

  3. using ajax parts of your web-site become more independent

  4. iframes, but probably this is not the case

这篇关于多个模型视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆