首页 > 解决方案 > 每个部分 c# dotnet 核心的多个模型

问题描述

<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; }
    }
}

}

标签: c#asp.net-mvcasp.net-core.net-coreasp.net-mvc-partialview

解决方案


要在同一页面中使用三个模型,您可以将这些模型放入如下视图模型中:

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>

部分观点:

  1. _ChildInfo.cshtml

    @model Test1
    <h1>_ChildInfo</h1>
    @Model.Name
    
  2. _ChildReg.cshtml

    @model Test2
    <h1>_ChildReg</h1>
    @Model.Age
    
  3. _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);
}

更新:

仪表板.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..
}

推荐阅读