Multiple models in a view

前端 未结 12 1833
太阳男子
太阳男子 2020-11-21 23:07

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

e.g.

pub         


        
12条回答
  •  孤独总比滥情好
    2020-11-21 23:24

    My advice is to make a big view model:

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

    In your Index.cshtml, if for example you have 2 partials:

    @addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers
    @model .BigViewModel
    
    @await Html.PartialAsync("_LoginViewPartial", Model.LoginViewModel)
    
    @await Html.PartialAsync("_RegisterViewPartial ", Model.RegisterViewModel )
    

    and in controller:

    model=new BigViewModel();
    model.LoginViewModel=new LoginViewModel();
    model.RegisterViewModel=new RegisterViewModel(); 
    

提交回复
热议问题