每个部分c#dotnet核心有多个模型
我的仪表板上有3个选项卡。我似乎找不到一种方法将3个不同的模型分配给每个部分,因为它们都是从同一路线调用的 编辑:根据要求添加代码每个部分c#dotnet核心有多个模型,c#,asp.net-mvc,asp.net-core,.net-core,asp.net-mvc-partialview,C#,Asp.net Mvc,Asp.net Core,.net Core,Asp.net Mvc Partialview,我的仪表板上有3个选项卡。我似乎找不到一种方法将3个不同的模型分配给每个部分,因为它们都是从同一路线调用的 编辑:根据要求添加代码 <div class="tab-content" id="nav-tabContent"> <div class="tab-pane fade" id="contactinfo" role="tabpanel" aria-labelledby
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade" id="contactinfo" role="tabpanel" aria-labelledby="contactinfo-tab"><partial name="_ChildInfo"></partial></div>
<div class="tab-pane fade show active" id="registerNewChild" role="tabpanel" aria-labelledby="registerNewChild-tab"><partial name="_ChildReg"></div>
<div class="tab-pane fade" id="yourkids" role="tabpanel" aria-labelledby="yourkids-tab"><partial name="_YourKidsTab"></partial></div>
</div>
//cshtml
@{
如果(型号!=null)
{
foreach(模型中的参与者i)
{
@i、 参与者姓名
}
}
if(Model==null)
{
空的
}
}
测试123
//视图模型
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用System.Linq;
使用Microsoft.EntityFrameworkCore;
名称空间LeagueProject.Models
{
公共类视图模型
{
私有上下文数据库;
公共视图模型(上下文)
{
db=上下文;
}
公共参与者{get;set;}
公共列表所有参与者{get;set;}
//列出allParticipants=db.Participants.Include(i=>i.Parent.ToList();
公开课参与者
{
[关键]
公共int ParticipantId{get;set;}
[必需(ErrorMessage=“is Required”)]
[MinLength(2,ErrorMessage=“必须至少包含2个字符”)]
公共字符串ParticipantFirstName{get;set;}
[必需(ErrorMessage=“is Required”)]
[MinLength(2,ErrorMessage=“必须至少包含2个字符”)]
公共字符串ParticipantLastName{get;set;}
[必需(ErrorMessage=“is Required”)]
公共字符串ParticipantGender{get;set;}
//[必需(ErrorMessage=“is Required”)]
//[范围(8,20,ErrorMessage=“如果roages 8-19,则此联盟”)]
//公共int参与{get;set;}
[必需(ErrorMessage=“需要出生日期”)]
[数据类型(DataType.Date)]
public System.DateTime ParticipantDOB{get;set;}
public int UserId{get;set;}
公共用户父项{get;set;}
公共列表所有联盟{get;set;}
}
}
}对于在同一页面中使用三个模型,您可以将这些模型放置到视图模型中,如下所示:
//viewModel
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace LeagueProject.Models
{
public class ViewModel
{
private Context db;
public ViewModel(Context context)
{
db = context;
}
public Participant participant { get; set; }
public List<Participant> allParticipants { get; set; }
// List<Participant> allParticipants = db.Participants.Include(i=>i.Parent).ToList();
public class Participant
{
[Key]
public int ParticipantId { get; set; }
[Required(ErrorMessage = "is required")]
[MinLength(2, ErrorMessage = "must be at least 2 characters")]
public string ParticipantFirstName { get; set; }
[Required(ErrorMessage = "is required")]
[MinLength(2, ErrorMessage = "must be at least 2 characters")]
public string ParticipantLastName { get; set; }
[Required(ErrorMessage = "is required")]
public string ParticipantGender { get; set; }
// [Required(ErrorMessage = "is required")]
// [Range(8, 20, ErrorMessage="this league if roages 8-19")]
// public int ParticipantAge { get; set; }
[Required(ErrorMessage="Need a date of birth")]
[DataType(DataType.Date)]
public System.DateTime ParticipantDOB { get; set; }
public int UserId { get; set; }
public User Parent { get; set; }
public List<MMLeagueParticipant> allLeagues { get; set; }
}
}
公共类MyViewModel
{
公共Test1 Test1{get;set;}
公共Test2 Test2{get;set;}
公共列表Test3{get;set;}
}
公共类Test1
{
公共字符串名称{get;set;}
}
公共类Test2
{
公共字符串年龄{get;set;}
}
公共类Test3
{
公共字符串类{get;set;}
}
视图(Index.cshtml):
@model MyViewModel
局部视图:
@model MyViewModel
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="contactinfo" role="tabpanel" aria-labelledby="contactinfo-tab">
<partial name="_ChildInfo" model="@Model.Test1"/>
</div>
<div class="tab-pane fade show active" id="registerNewChild" role="tabpanel" aria-labelledby="registerNewChild-tab">
<partial name="_ChildReg" model="@Model.Test2"/>
</div>
<div class="tab-pane fade show active" id="yourkids" role="tabpanel" aria-labelledby="yourkids-tab">
<partial name="_YourKidsTab" model="@Model.Test3"/>
</div>
</div>
@model Test1
_儿童信息
@型号.名称
@model Test1
<h1>_ChildInfo</h1>
@Model.Name
@model Test2
_ChildReg
@模型年龄
@model Test2
<h1>_ChildReg</h1>
@Model.Age
@型号列表
_你的孩子
@foreach(模型中的var项目)
{
@项目.类别
}
@model List<Test3>
<h1>_YourKidsTab</h1>
@foreach(var item in Model)
{
@item.Class
}
[HttpGet]
公共IActionResult索引()
{
var模型=新的MyViewModel()
{
Test1=newtest1(){Name=“aa”},
Test2=newtest2(){Age=“23”},
Test3=new List(){new Test3(){Class=“Class2”}
};
返回视图(模型);
}
更新:
@model ViewModel
<partial name="_YourKidsTab" model="@Model.allParticipants "/>
@model List<Participant>
//....
Dashboard.cshtml:
[HttpGet]
public IActionResult Index()
{
var model = new MyViewModel()
{
Test1 = new Test1() { Name = "aa" },
Test2 = new Test2() { Age = "23" },
Test3 = new List<Test3>(){new Test3() { Class = "Class2" }}
};
return View(model);
}
@model视图模型
\u YourKidsTab.cshtml:
@model ViewModel
<partial name="_YourKidsTab" model="@Model.allParticipants "/>
@model List<Participant>
//....
@型号列表
//....
控制器:
@model ViewModel
<partial name="_YourKidsTab" model="@Model.allParticipants "/>
@model List<Participant>
//....
[HttpGet]
[路线(“仪表板”)]
公共IActionResult仪表板()
{
if(HttpContext.Session.GetInt32(“UserId”)==null)
{
返回重定向到操作(“索引”、“主页”);
}
ViewBag.loggedinUser=HttpContext.Session.GetInt32(“用户ID”);
HttpContext.Session.SetInt32(“仪表板选项卡”,0);
列出allParticipants=db.Participants.Include(i=>i.Parent.ToList();
//加上这个。。。。。。
ViewModel模型=新的ViewModel()
{
所有参与者=所有参与者
};
返回视图(“仪表板”,模型);//在此处更改。。
}
对于在同一页面中使用三个模型,您可以将这些模型放置到视图模型中,如下所示:
//viewModel
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace LeagueProject.Models
{
public class ViewModel
{
private Context db;
public ViewModel(Context context)
{
db = context;
}
public Participant participant { get; set; }
public List<Participant> allParticipants { get; set; }
// List<Participant> allParticipants = db.Participants.Include(i=>i.Parent).ToList();
public class Participant
{
[Key]
public int ParticipantId { get; set; }
[Required(ErrorMessage = "is required")]
[MinLength(2, ErrorMessage = "must be at least 2 characters")]
public string ParticipantFirstName { get; set; }
[Required(ErrorMessage = "is required")]
[MinLength(2, ErrorMessage = "must be at least 2 characters")]
public string ParticipantLastName { get; set; }
[Required(ErrorMessage = "is required")]
public string ParticipantGender { get; set; }
// [Required(ErrorMessage = "is required")]
// [Range(8, 20, ErrorMessage="this league if roages 8-19")]
// public int ParticipantAge { get; set; }
[Required(ErrorMessage="Need a date of birth")]
[DataType(DataType.Date)]
public System.DateTime ParticipantDOB { get; set; }
public int UserId { get; set; }
public User Parent { get; set; }
public List<MMLeagueParticipant> allLeagues { get; set; }
}
}
公共类MyViewModel
{
公共Test1 Test1{get;set;}
公共Test2 Test2{get;set;}
公共列表Test3{get;set;}
}
公共类Test1
{
公共字符串名称{get;set;}
}
公共类Test2
{
公共字符串年龄{get;set;}
}
公共类Test3
{
公共字符串类{get;set;}
}
视图(Index.cshtml):
@model MyViewModel
局部视图:
@model MyViewModel
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="contactinfo" role="tabpanel" aria-labelledby="contactinfo-tab">
<partial name="_ChildInfo" model="@Model.Test1"/>
</div>
<div class="tab-pane fade show active" id="registerNewChild" role="tabpanel" aria-labelledby="registerNewChild-tab">
<partial name="_ChildReg" model="@Model.Test2"/>
</div>
<div class="tab-pane fade show active" id="yourkids" role="tabpanel" aria-labelledby="yourkids-tab">
<partial name="_YourKidsTab" model="@Model.Test3"/>
</div>
</div>
@model Test1
_儿童信息
@型号.名称
@model Test1
<h1>_ChildInfo</h1>
@Model.Name
@model Test2
_ChildReg
@模型年龄
@model Test2
<h1>_ChildReg</h1>
@Model.Age
@型号列表
_你的孩子
@foreach(模型中的var项目)
{
@项目.类别
}
@model List<Test3>
<h1>_YourKidsTab</h1>
@foreach(var item in Model)
{
@item.Class
}
[HttpGet]
公共IActionResult索引()
{
var模型=新的MyViewModel()
{
Test1=newtest1(){Name=“aa”},
Test2=newtest2(){Age=“23”},
Test3=new List(){new Test3(){Class=“Class2”}
};
返回视图(模型);
}
更新:
@model ViewModel
<partial name="_YourKidsTab" model="@Model.allParticipants "/>
@model List<Participant>
//....
Dashboard.cshtml:
[HttpGet]
public IActionResult Index()
{
var model = new MyViewModel()
{
Test1 = new Test1() { Name = "aa" },
Test2 = new Test2() { Age = "23" },
Test3 = new List<Test3>(){new Test3() { Class = "Class2" }}
};
return View(model);
}
@model视图模型
\u YourKidsTab.cshtml:
@model ViewModel
<partial name="_YourKidsTab" model="@Model.allParticipants "/>
@model List<Participant>
//....
@型号列表
//....
控制器:
@model ViewModel
<partial name="_YourKidsTab" model="@Model.allParticipants "/>
@model List<Participant>
//....
[HttpGet]
[路线(“仪表板”)]
公共IActionResult仪表板()
{
if(HttpContext.Session.GetInt32(“UserId”)==null)
{
返回重定向到操作(“索引”、“主页”);
}
ViewBag.loggedinUser=HttpContext.Session.GetInt32(“用户ID”);
HttpContext.Session.SetInt32(“仪表板选项卡”,0);
列出allParticipants=db.Participants.Include(i=>i.Parent.ToList();
//加上这个。。。。。。
ViewModel模型=新的ViewModel()
{
所有参与者=所有参与者
};
返回视图(“仪表板”,模型);//在此处更改。。
}
我查看了文档,但没有找到一个