问题描述
我有2个表Work_table
和Employee_table
.我想在Employee_table
的索引视图中显示Work_table
中的emp_id
和Employee_table
中的相应emp_name
.我的模型是:
I have 2 tables Work_table
and Employee_table
.I want to display emp_id
from Work_table
and corresponding emp_name
from Employee_table
in the index view of Employee_table
.my models are:
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; }
}
}
如何编写控制器?在控制器中编写函数时卡住了.
How to write controller?I stucked when writing the function in controller.
public ActionResult Index()
{
}
请提出解决方案.预先感谢.
Please suggest a solution. thanks in advance.
致谢
推荐答案
我之前是错的,我从您的代码中发现Employee
和Work
表之间存在多对多的关系.为了方便起见,我将Job
用作Work
表/模型的名称.
I was wrong earlier, I made out from your code that there is a many -to- many relationship between Employee
and Work
tables. For my convenience, I have used Job
as the name for your Work
table / model.
我希望您在索引视图中显示EmployeeIds
的列表以及相应的EmployeeNames
.我在viewmodel中添加了一个名为JobName
的额外属性,您也可以具有其他属性.
I hope that you want to display a list of EmployeeIds
along with the corresponding EmployeeNames
in the Index View. I have added an extra property called JobName
to viewmodel, you can have other properties too.
为此,创建一个ViewModel EmployeeViewModel
并将动作结果的index view
与IEnumerable<EmployeeViewModel>
绑定. EmployeeViewModel
的定义可以类似于-
For that matter, create a ViewModel EmployeeViewModel
and bind the index view
of your action result with an IEnumerable<EmployeeViewModel>
. The definition for EmployeeViewModel
can be something like -
public class EmployeeViewModel
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string JobName { get; set; }
//..Other memberVariables..
}
假设这些是您的模型-
Employee
Suppose these are your models -
Employee
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; }
}
然后WorkTable
,为了方便起见,将其重新命名为
And WorkTable
, renamed it as Job
for my own convenience
public class Job
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int JobId { get; set; }
public string JobName { get; set; }
public JobCategory JobCategory { get; set; }
public int EmployeeId { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
}
在索引操作"中,通过连接两个表来创建结果集,将其绑定到IEnumerable<EmployeeViewModel>
并将其作为模型传递给视图.正如我之前提到的,View应该收到IEnumerable<EmployeeViewModel>
类型的模型,因此您需要查询类似于-
In your Index Action, you create a result set by joining the two tables, bind it to IEnumerable<EmployeeViewModel>
and pass that as a model to the view. The View should receive a model of type IEnumerable<EmployeeViewModel>
as I mentioned earlier,so you need to query your entities which should be something like -
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);
}
您的索引视图应类似于-
Your index view should be something like -
@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>
在上述解决方案中,我尝试过产生与您类似的情况并解决您的担忧.我希望这会给您带来一些缓解的忧虑,并帮助您前进.以此为地图,然后尝试按照路线找到自己的目的地/解决方案.顺便说一句,对于此延迟的回复,我们深表歉意.希望这会有所帮助.
这篇关于从不同的表中获取数据并在mvc4的“视图索引"中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!