MVC - 在视图中引用两个模型的问题 [英] MVC - problem referring two models in a view

查看:200
本文介绍了MVC - 在视图中引用两个模型的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


你好,



我是MVC的学习者,有在一个视图中引用2个不同的模型存在疑问。



我有2个名为'tbl_employee'和'tbl_Compoents'的表。



声明一个Model类,如下所示,具有2个表的属性。



Hello there,

I'm a learner in MVC and have a doubt in referring 2 different models in a single View.

I have 2 tables called 'tbl_employee' and 'tbl_Compoents'.

Declared a Model class as below, with the proprties of 2 tables.

public class EmpModel
    {
        MVCEntities Emp = new MVCEntities();
        [Required]
        public string EmployeeNumber { get; set; }
        [DataType(DataType.Password)]
        [Required]
        public string password { get; set; }
        public string EmployeeName { get; set; }
        public string curproject { get; set; }
        public string Projectcode { get; set; }
        public string curprojectmanager { get; set; }
        public string Designation { get; set; }
        public Boolean manager { get; set; }
        public int man_per { get; set; }
        public string pop3_mailid { get; set; }
        public string levelc { get; set; }
        public Boolean IsActive { get; set; }


        public string DisplayText { get; set; }
        public string DisplayComponent { get; set; }



}





我正在做的是,阅读来自tbl_Compoents表的数据,并相应地动态创建组件。



DisplayText DisplayComponent

员工姓名文本框

员工编号文本框

性别下拉列表

指定文本框

项目代码下拉列表

状态复选框

mailID文本框

经理下拉列表





所以,我使用了部分查看在另一个视图上动态创建的组件,我的部分视图模型如下所示(我可以通过仅引用此模型中的tbl_Components表来阅读和获取组件)



公共课EmpBO

{

MVCEntities Emp =新MVCEntities();



//一些将员工数据插入数据库所需的其他类

public IEnumerable< ; tbl_components> GetCompList()

{

var obj = Emp.tbl_Components;

return obj;

}

}



但总的来说,当我想将整个视图中的数据保存到tbl_employee表中时,我需要参考下面的模型在我的视图中。



我甚至尝试将tbl_Componets和empModel组合到一个模型类中。但没有运气。有人可以帮我吗?


}


What I'm doing is, reading the data from tbl_Compoents table and accordingly creating the components dynamically.

DisplayText DisplayComponent
Employee Name textbox
Employee Number textbox
gender dropdownlist
Designation textbox
Projectcode dropdownlist
Status checkbox
mailID textbox
manager dropdownlist


So, I'm using a partial view for the components to create dynamically on another view and my Model for the partial view is as below(I can read and get the components by only referring tbl_Components table in this model)

public class EmpBO
{
MVCEntities Emp = new MVCEntities();

// some other classes required to insert employee data into db
public IEnumerable<tbl_components> GetCompList()
{
var obj = Emp.tbl_Components;
return obj;
}
}

But as whole, when I want to save the data on the entire view into tbl_employee table, I need to refer to the model as below in my View.

I have even tried combining the tbl_Componets and empModel into a single model class. But no luck. can anyone help me on this please?

推荐答案

您好,

您不能同时将两个不同的模型传递给同一个视图。要解决这个问题,你需要将这两个类包装成单个Model类,如下所示



Hello,
You can not pass two different models to same view at same time. To overcome this issue, you need to wrap this two classes into single Model class as below

namespace MvcProject.Models {
     public class FirstModel : IEnumerable<firstmodel>, IList<firstmodel> {
         public string Title { get; set; }
         public string Message { get; set; }
         ...
     }
     public class SecondModel : IEnumerable<secondmodel>, IList<secondmodel> {
         public string Title { get; set; }
         public string Message { get; set; }
         ...
     }
 
     /*Strongly typed wrapper model*/
     public class WrapperModel {
          FirstModel firstModel;
          SecondModel secondModel;
          public WrapperModel() {
                 firstModel = new FirstModel();
                 secondModel = new SecondModel();
          }
     }
}</secondmodel></secondmodel></firstmodel></firstmodel>


是的,我已经这样做了。但是如何在视图中引用它?
Yes, I have done this. But how can I refer this in the View?


这篇关于MVC - 在视图中引用两个模型的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆