gpt4 book ai didi

asp.net-mvc-4 - 从不同的表中获取数据并在mvc4中的View Index中显示

转载 作者:行者123 更新时间:2023-12-02 20:56:38 25 4
gpt4 key购买 nike

我有 2 张 table Work_tableEmployee_table .我想显示emp_id来自Work_table以及相应的emp_name来自Employee_tableEmployee_table 的索引 View 中。我的模型是:

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; }


}

}

如何编写 Controller ?在 Controller 中编写函数时我陷入困境。

public ActionResult Index()
{
}

请提出解决方案。提前致谢。

问候

最佳答案

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

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

就此而言,创建一个 ViewModel EmployeeViewModel并绑定(bind)index view您的行动结果为 IEnumerable<EmployeeViewModel>EmployeeViewModel 的定义可以是这样的 -

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

public string EmployeeName { get; set; }

public string JobName { get; set; }

//..Other memberVariables..
}


假设这些是你的模型 -
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重命名Job为了我自己的方便

    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; }
}

在索引操作中,您通过连接两个表来创建结果集,并将其绑定(bind)到 IEnumerable<EmployeeViewModel>并将其作为模型传递给 View 。 View 应该接收 IEnumerable<EmployeeViewModel> 类型的模型正如我之前提到的,所以你需要查询你的实体,它应该是这样的 -

    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);
}

你的索引 View 应该是这样的 -

@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>

在上述解决方案中,我尝试生成与您类似的情况并解决您的担忧。我希望这能为您带来某种程度的缓解,并帮助您继续前进。将此作为 map 并尝试按照路线找到您自己的目的地/解决方案。顺便说一句,很抱歉回复延迟。希望这会有所帮助。

关于asp.net-mvc-4 - 从不同的表中获取数据并在mvc4中的View Index中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22037603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com