Asp.net mvc 4 从不同的表中获取数据,并将其显示在mvc4的视图索引中

Asp.net mvc 4 从不同的表中获取数据,并将其显示在mvc4的视图索引中,asp.net-mvc-4,Asp.net Mvc 4,我有两个表Work\u table和Employee\u table。我想在Employee\u table的索引视图中显示emp\u idfromWork\u table和相应的emp\u namefromEmployee\u table。 我的模型是: namespace MvcConQuery.Models { [Table("Work_Table")] public class EnquiryModel { [Key] [DatabaseGeneratedAttribute(Databa

我有两个表
Work\u table
Employee\u table
。我想在
Employee\u table
的索引视图中显示
emp\u id
from
Work\u table
和相应的
emp\u name
from
Employee\u table
。 我的模型是:

namespace MvcConQuery.Models
{
[Table("Work_Table")]
public class EnquiryModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Int32 Enq_id { get; set; }
[Required]
[Display(Name="Name")]
public string CustomerName { get; set; }
[ReadOnly(true)]
public string Date
{
get
{
DateTime Date = DateTime.Now;
return Date.ToString("yyyy-MM-dd"); ;
}
set{}
}
[Required]
[Display(Name = "Region")]
public string Region { get; set; }
[Required]
[RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Entered phone number format is not valid.")]
[Display(Name = "Phone number")]
public string Ph_No { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email_id")]
public string Email_id { get; set; }
[Required]
[Display(Name = "Address")]
public string Address { get; set; }
[Required]
[Display(Name = "Query")]
public string Query { get; set; }
public string Referral { get; set; }
public string Feedback { get; set; }
public string Status { get; set; }
public Int32? Emp_id { get; set; }
public string FollowUpDate { get; set; }
public List<EmployeeModel> Employees { get; set; }
}}


namespace MvcConQuery.Models
{
[Table("Employee_Table")]
public class EmployeeModel
{

[Key,Column(Order=0)]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
//[ForeignKey("EnquiryModel")]
public Int32 Emp_id { get; set; }
public string Emp_Name{ get; set; }
//[Key,Column(Order=1)]
public string Region { get; set; }
//[ForeignKey("Region")]
public string Emp_PhNo { get; set; }
public string Emp_Address { get; set; }
public List<EnquiryModel> Enquires { get; set; }


}

}
请提出解决办法。提前谢谢


关于

我之前错了,我从您的代码中看出,
员工
工作
表之间存在多对多关系。为方便起见,我使用了
Job
作为您的
工作
表/模型的名称

我希望您希望在索引视图中显示
employeeid的列表
,以及相应的
EmployeeNames
。我在viewmodel中添加了一个名为
JobName
的额外属性,您还可以拥有其他属性

为此,创建一个ViewModel
EmployeeViewModel
,并将操作结果的
索引视图
绑定到一个
IEnumerable
EmployeeViewModel
的定义如下-

    public class EmployeeViewModel
    {
        public int EmployeeId { get; set; }

        public string EmployeeName  { get; set; }

        public string JobName { get; set; }

        //..Other memberVariables..
    }

假设这些是你的模型-
员工

        public class Employee
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int EmployeeId { get; set; }

            public string EmployeeName { get; set; }

            public string Address { get; set; }

            public virtual ICollection<Job> Jobs { get; set; }
        }
在索引操作中,通过连接两个表来创建结果集,将其绑定到
IEnumerable
,并将其作为模型传递给视图。正如我前面提到的,视图应该接收类型为
IEnumerable
的模型,因此您需要查询实体,该实体应该类似于-

    public ActionResult Index()
    {
        //..something like this..this is IQueryable..
        //...convert this to IEnumerable and send this as the model to ..
        //..the Index View as shown below..here you are querying your own tables, 
        //.. Employee and Job,and binding the result to the EmployeeViewModel which
        //.. is passed on to the Index view.
        IEnumerable<EmployeeViewModel> model=null;
        model = (from e in db.Employees
                    join j in db.Jobs on e.EmployeeId equals j.EmployeeId
                    select new EmployeeViewModel
                    {
                        EmployeeId = e.EmployeeId,
                        EmployeeName = e.EmployeeName,
                        JobName = j.JobName
                    });

        return View(model);
    }
@model IEnumerable<MyApp.Models.EmployeeViewModel>

@{
    ViewBag.Title = "Index";
}


<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.EmployeeId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.EmployeeName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.JobName)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.EmployeeId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EmployeeName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.JobName)
        </td>
    </tr>
}

</table>
public ActionResult Index()
{
//…像这样的..这是我的。。
//…将其转换为IEnumerable并将其作为模型发送到。。
//..索引视图,如下所示..您正在查询自己的表,
//..员工和职务,并将结果绑定到EmployeeViewModel
//..被传递到索引视图。
IEnumerable模型=null;
模型=(来自数据库中的e.Employees
加入e.EmployeeId上的db.Jobs等于j.EmployeeId
选择新建EmployeeViewModel
{
EmployeeId=e.EmployeeId,
EmployeeName=e.EmployeeName,
JobName=j.JobName
});
返回视图(模型);
}
您的索引视图应该类似于-

    public ActionResult Index()
    {
        //..something like this..this is IQueryable..
        //...convert this to IEnumerable and send this as the model to ..
        //..the Index View as shown below..here you are querying your own tables, 
        //.. Employee and Job,and binding the result to the EmployeeViewModel which
        //.. is passed on to the Index view.
        IEnumerable<EmployeeViewModel> model=null;
        model = (from e in db.Employees
                    join j in db.Jobs on e.EmployeeId equals j.EmployeeId
                    select new EmployeeViewModel
                    {
                        EmployeeId = e.EmployeeId,
                        EmployeeName = e.EmployeeName,
                        JobName = j.JobName
                    });

        return View(model);
    }
@model IEnumerable<MyApp.Models.EmployeeViewModel>

@{
    ViewBag.Title = "Index";
}


<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.EmployeeId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.EmployeeName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.JobName)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.EmployeeId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EmployeeName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.JobName)
        </td>
    </tr>
}

</table>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
@DisplayNameFor(model=>model.EmployeeId)
@DisplayNameFor(model=>model.EmployeeName)
@DisplayNameFor(model=>model.JobName)
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.EmployeeId)
@DisplayFor(modelItem=>item.EmployeeName)
@DisplayFor(modelItem=>item.JobName)
}

在上述解决方案中,我试图创造一种类似于您的情况,并解决您的问题。我希望这能缓解你的担忧,帮助你前进。将此作为一张地图,尝试按照路线找到自己的目的地/解决方案。顺便说一句,很抱歉延迟回复。希望这有帮助。

此代码缺少某些内容选择新的EmployeeviewModel


你被困在哪里?你的企图是什么?共享您的代码并让我们知道。我在controller中编写函数public ActionResult Index(){}时遇到了问题。是否可以使用其他ViewModel类来连接不同表中的数据?是的,您需要创建一个
ViewModel
(根据需要使用属性)来将数据呈现给视图。在
索引
操作中,您需要连接两个表
工作
员工
。据我所知,你应该在
工作
员工
之间建立一种1对*的关系。因此,在索引操作中,您需要启动一个查询,将
EmployeeId
上的这两个表连接起来,然后将数据绑定到
ViewModel
中的相应属性,然后将其传递给视图。我将尝试设置一些内容。控制器中出现错误:无法将类型“System.Linq.IQueryable”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换(是否缺少转换?)。是的,这就是我注释的内容。
IQuertable
,您需要将其转换为
IEnumerable
@Nivya。主要思想是在那里执行逻辑或从
ActionResult
中的存储库调用方法。是的,我转换它,现在它正在工作。非常感谢。访问已经定义好的SQL视图怎么样?我不会直接读取所有表。我只需要阅读一个视图,没有更新<代码>modelBuilder.Entity().ToTable(“vw_数据子任务循环”)
model = (from e in db.Employees
                    join j in db.Jobs on e.EmployeeId equals j.EmployeeId
                    select new EmployeeViewModel
                    {
                        EmployeeId = e.EmployeeId,
                        EmployeeName = e.EmployeeName,
                        JobName = j.JobName
                    });