问题描述
<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>
我的仪表板上有 3 个标签。我似乎无法找到将 3 个不同模型分配给每个部分的方法,因为它们都是从同一路径调用的。
///////////////////////cshtml
@{
if(Model != null)
{
foreach(Participant i in Model)
{
<p>@i.ParticipantFirstName</p>
}
}
if(Model == null)
{
<p>null?</p>
}
} 测试123
//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; }
}
}
}
解决方法
要在同一页面中使用三个模型,您可以将这些模型放入如下所示的视图模型中:
public class MyViewModel
{
public Test1 Test1 { get; set; }
public Test2 Test2 { get; set; }
public List<Test3> Test3 { get; set; }
}
public class Test1
{
public string Name { get; set; }
}
public class Test2
{
public string Age { get; set; }
}
public class Test3
{
public string Class { get; set; }
}
视图(索引.cshtml):
@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>
部分观点:
-
_ChildInfo.cshtml
@model Test1 <h1>_ChildInfo</h1> @Model.Name
-
_ChildReg.cshtml
@model Test2 <h1>_ChildReg</h1> @Model.Age
-
_YourKidsTab.cshtml
@model List<Test3> <h1>_YourKidsTab</h1> @foreach(var item in Model) { @item.Class }
控制器:
[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);
}
更新:
Dashboard.cshtml:
@model ViewModel
<partial name="_YourKidsTab" model="@Model.allParticipants "/>
_YourKidsTab.cshtml:
@model List<Participant>
//....
控制器:
[HttpGet]
[Route("dashboard")]
public IActionResult Dashboard()
{
if(HttpContext.Session.GetInt32("UserId") == null)
{
return RedirectToAction("Index","Home");
}
ViewBag.loggedinUser = HttpContext.Session.GetInt32("UserId");
HttpContext.Session.SetInt32("DashboardTab",0);
List<Participant> allParticipants = db.Participants.Include(i=>i.Parent).ToList();
//add this ......
ViewModel model = new ViewModel()
{
allParticipants = allParticipants
};
return View("Dashboard",model); //change here..
}