将多个模型传递到视图MVC中

时间:2014-05-09 02:23:34

标签: 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; }
[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; }
    }
}

的DbContext

公共类UnitedContext:DbContext     {

    public UnitedContext() : base("name=UnitedContext")
    {
    }

    public System.Data.Entity.DbSet<United.Models.Child> Children { get; set; }
    public System.Data.Entity.DbSet<United.Models.ChildEval> ChildEvals { get; set; }

}

视图模型:

public class ChildViewModel
    {
        public class Child
        {
            public string FirstName { get; set; }
        }
        public class ChildEval
        {
            public int PtaRight { get; set; }
        }
    }

ViewModelController? :

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

            return View();
        }
    }
}

我坚持如何为viewmodel创建控制器以及如何实际将数据导入视图。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

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

由于您正在处理孩子,我现在称其为ChildrenController。

通常一个好主意是将2个模型包装到一个视图模型中并使用它。在这种情况下,Model!= 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);
    }
}

查看:

@model ChildViewModel

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