Asp.net mvc 将多个模型传递到视图MVC

Asp.net mvc 将多个模型传递到视图MVC,asp.net-mvc,Asp.net Mvc,对MVC来说相对较新,无法理解如何将多个模型中的数据传递到单个视图中。我知道我需要使用视图模型,但我不知道在控制器中该做什么 型号: public class Child { [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] public int ChartNumber { get; set; } [Display(Name = "First Name")] public string FirstName { get; set; } [

对MVC来说相对较新,无法理解如何将多个模型中的数据传递到单个视图中。我知道我需要使用视图模型,但我不知道在控制器中该做什么

型号:

public class Child
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ChartNumber { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
public string City { get; set; }
public string State { get; set; }
[Display(Name = "Zip Code")]
public int ZipCode { get; set; }
public string Ethnicity { get; set; }
public string Referral { get; set; }
[Display(Name = "Recommended Re-evaluation")]
public bool ReEval { get; set; }
[Display(Name = "Hearing Aid Candidate")]
public bool HaCandidate { get; set; }
[Display(Name = "Fit With Hearing Aid")]
public bool FitHa { get; set; }

public virtual List<ChildEval> ChildEvals { get; set; }
}

public class ChildEval
{
[Key]
public int ChildEvalId { get; set; }
public DateTime Date { get; set; }
        public int PtaRight { get; set; }
        public int PtaLeft { get; set; }
        public int UnaidedSiiRight { get; set; }
        public int UnaidedSiiLeft { get; set; }
        public int AidedSiiRight { get; set; }
        public int AidedSiiLeft { get; set; }
        public int ChartNumber { get; set; }

        public virtual Child Child { get; set; }
    }
}
ViewModelController?:


我一直在研究如何为viewmodel创建控制器,以及如何将数据实际导入视图。任何帮助都将不胜感激

没有名为ViewModelController的控制器。控制器是数据库和视图之间的处理程序。因此,控制器的名称应该告诉程序员控制器控制的是哪种数据。默认情况下,它还指示您在web应用程序中的子网站(ViewModelController将生成
http://mysite/ViewModel/

既然你在处理孩子们,我现在就称之为ChildrenController

通常,一个好主意是将两个模型包装成一个viewmodel并使用它。在这种情况下,模型!=ViewModel,model是实体的数据库模型,而ViewModel是传递给视图或从视图传递过来的模型

public class Child
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
public class SomeOtherModel
{
    public int ID { get; set; }
    public int SomeInteger { get; set; }
    public int SomeOtherInteger { get; set; }
}

public class ChildViewModel
{
    public Child Child { get; set; }
    public SomeOtherModel SomeOtherModel { get; set; }

    public ChildViewModel(Child child, SomeOtherModel s)
    {
        Child = child;
        SomeOtherModel = s;
    }
}
在控制器中

public class ChildrenController : Controller
{
    //
    // GET: /Children/
    public ActionResult Index()
    {
        Child child = /*Fetch the child from database*/;
        SomeOtherModel someOther =  = /*Fetch something else from database*/;

        ChildViewModel model = new ChildViewModel(child,someOther);

        return View(model);
    }
}
视图:

   public class ViewModelController : Controller
    {
        //
        // GET: /ViewModel/
        public ActionResult Index()
        {

            return View();
        }
    }
}
public class Child
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
public class SomeOtherModel
{
    public int ID { get; set; }
    public int SomeInteger { get; set; }
    public int SomeOtherInteger { get; set; }
}

public class ChildViewModel
{
    public Child Child { get; set; }
    public SomeOtherModel SomeOtherModel { get; set; }

    public ChildViewModel(Child child, SomeOtherModel s)
    {
        Child = child;
        SomeOtherModel = s;
    }
}
public class ChildrenController : Controller
{
    //
    // GET: /Children/
    public ActionResult Index()
    {
        Child child = /*Fetch the child from database*/;
        SomeOtherModel someOther =  = /*Fetch something else from database*/;

        ChildViewModel model = new ChildViewModel(child,someOther);

        return View(model);
    }
}
@model ChildViewModel

@Html.EditorFor(model => model.Child.FirstName)
@Html.EditorFor(model => model.Child.LastName)