Asp.net mvc 5 需要在MVC 5中的数据表中显示名称而不是GUID的帮助吗
我需要一些帮助来显示MVC5中数据表中的名称而不是GUID 我通过以下教程实现了DataTable: 我试图显示一个市政区域、市政和客户状态的名称。它在我的详细信息视图模型中显示得非常好,但是它在DataTable中显示GUID。我花了好几天的时间试图弄明白这一点,但都无济于事 以下是控制器中DataTable的代码:Asp.net mvc 5 需要在MVC 5中的数据表中显示名称而不是GUID的帮助吗,asp.net-mvc-5,datatables,Asp.net Mvc 5,Datatables,我需要一些帮助来显示MVC5中数据表中的名称而不是GUID 我通过以下教程实现了DataTable: 我试图显示一个市政区域、市政和客户状态的名称。它在我的详细信息视图模型中显示得非常好,但是它在DataTable中显示GUID。我花了好几天的时间试图弄明白这一点,但都无济于事 以下是控制器中DataTable的代码: public ActionResult Get([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest reques
public ActionResult Get([ModelBinder(typeof(DataTablesBinder))] IDataTablesRequest requestModel, ClientsAdvancedSearchViewModel searchViewModel)
{
IQueryable<Client> query = DbContext.Clients;
var totalCount = query.Count();
// searching and sorting
query = SearchClients(requestModel, searchViewModel, query);
var filteredCount = query.Count();
// Paging
query = query.Skip(requestModel.Start).Take(requestModel.Length);
var data = query.Select(client => new
{
ClientIdentifier = client.ClientIdentifier,
CompanyName = client.CompanyName,
ClientContactPerson = client.ClientContactPerson,
ClientEmail = client.ClientEmail,
ClientTel = client.ClientTel,
Consultant = client.Consultant,
Town = client.Town,
Suburb = client.Suburb,
MunicipalRegionID = client.MunicipalRegionID,
MunicipalityID = client.MunicipalityID,
ClientStatusID = client.ClientStatusID,
}).ToList();
return Json(new DataTablesResponse(requestModel.Draw, data, filteredCount, totalCount), JsonRequestBehavior.AllowGet);
}
public ActionResult Get([ModelBinder(typeof(datatablebinder))]IDataTablesRequestModel,ClientsAdvancedSearchViewModel searchViewModel)
{
IQueryable query=DbContext.Clients;
var totalCount=query.Count();
//搜索和排序
query=SearchClients(requestModel、searchViewModel、query);
var filteredCount=query.Count();
//寻呼
query=query.Skip(requestModel.Start).Take(requestModel.Length);
var data=query.Select(客户端=>new
{
ClientIdentifier=client.ClientIdentifier,
CompanyName=client.CompanyName,
ClientContactPerson=client.ClientContactPerson,
ClientEmail=client.ClientEmail,
ClientTel=client.ClientTel,
顾问=客户。顾问,
Town=客户。Town,
郊区=客户。郊区,
市政区域ID=客户端。市政区域ID,
市政府ID=客户。市政府ID,
ClientStatusID=client.ClientStatusID,
}).ToList();
返回Json(新的DataTablesResponse(requestModel.Draw、data、filteredCount、totalCount)、JsonRequestBehavior.AllowGet);
}
下面是我的客户机类的代码:
public class Client
{
[Key]
public System.Guid ClientIdentifier { get; set; }
public string CompanyName { get; set; }
public string SiteName { get; set; }
public string Status { get; set; }
public string Consultant { get; set; }
public string Comments { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> Date { get; set; }
public string ClientInformation { get; set; }
public string ClientContactPerson { get; set; }
public string ClientEmail { get; set; }
public string ClientTel { get; set; }
public string ClientAddress { get; set; }
public string GPSCoordinates { get; set; }
public string Town { get; set; }
public string Suburb { get; set; }
public System.Guid MunicipalRegionID { get; set; }
public System.Guid MunicipalityID { get; set; }
public System.Guid ClientStatusID { get; set; }
public string CreatedBy { get; set; }
public Nullable<System.DateTime> DateCreated { get; set; }
public string LastEditedBy { get; set; }
public Nullable<System.DateTime> DateLastEdited { get; set; }
}
公共类客户端
{
[关键]
public System.Guid客户端标识符{get;set;}
公共字符串CompanyName{get;set;}
公共字符串SiteName{get;set;}
公共字符串状态{get;set;}
公共字符串顾问{get;set;}
公共字符串注释{get;set;}
[数据类型(DataType.Date)]
[DisplayFormat(DataFormatString=“{0:yyyy-MM-dd}”,ApplyFormatInEditMode=true)]
公共可空日期{get;set;}
公共字符串ClientInformation{get;set;}
公共字符串ClientContactPerson{get;set;}
公共字符串ClientEmail{get;set;}
公共字符串ClientTel{get;set;}
公共字符串客户端地址{get;set;}
公共字符串GPSCoordinates{get;set;}
公共字符串{get;set;}
公共字符串{get;set;}
public System.Guid市政区域ID{get;set;}
public System.Guid市政ID{get;set;}
public System.Guid ClientStatusID{get;set;}
通过{get;set;}创建的公共字符串
公共可为空的DateCreated{get;set;}
公共字符串LastEditedBy{get;set;}
公共可空DateLastEdited{get;set;}
}
以下是我的市政区域类的代码:
public class MunicipalRegion
{
[Key]
public System.Guid MunicipalRegionIdentifier { get; set; }
public string MunicipalRegionName { get; set; }
public string CreatedBy { get; set; }
public Nullable<System.DateTime> DateCreated { get; set; }
public string LastEditedBy { get; set; }
public Nullable<System.DateTime> DateLastEdited { get; set; }
}
公共级市政区域
{
[关键]
public System.Guid市政区域标识符{get;set;}
公共字符串市政区域名称{get;set;}
通过{get;set;}创建的公共字符串
公共可为空的DateCreated{get;set;}
公共字符串LastEditedBy{get;set;}
公共可空DateLastEdited{get;set;}
}
以下是我的市政级别代码:
public class Municipality
{
[Key]
public System.Guid MunicipalityIdentifier { get; set; }
public string MunicipalityName { get; set; }
public System.Guid MunicipalRegionID { get; set; }
public string CreatedBy { get; set; }
public Nullable<System.DateTime> DateCreated { get; set; }
public string LastEditedBy { get; set; }
public Nullable<System.DateTime> DateLastEdited { get; set; }
公共类自治市
{
[关键]
public System.Guid市政标识符{get;set;}
公共字符串市政名称{get;set;}
public System.Guid市政区域ID{get;set;}
通过{get;set;}创建的公共字符串
公共可为空的DateCreated{get;set;}
公共字符串LastEditedBy{get;set;}
公共可空DateLastEdited{get;set;}
以下是“我的客户端状态”类的代码:
public class ClientStatus
{
[Key]
public System.Guid ClientStatusIdentifier { get; set; }
public string ClientStatusName { get; set; }
public string CreatedBy { get; set; }
public Nullable<System.DateTime> DateCreated { get; set; }
public string LastEditedBy { get; set; }
public Nullable<System.DateTime> DateLastEdited { get; set; }
}
公共类ClientStatus
{
[关键]
public System.Guid ClientStatusIdentifier{get;set;}
公共字符串ClientStatusName{get;set;}
通过{get;set;}创建的公共字符串
公共可为空的DateCreated{get;set;}
公共字符串LastEditedBy{get;set;}
公共可空DateLastEdited{get;set;}
}
非常感谢您的帮助。谢谢:)我可能错了,但听起来您试图在生成的视图中隐藏关键字段。这可以通过添加以下注释来完成
[显示(AutoGenerateField=false)]
好的,我明白你现在想做什么了。主要问题是你的类设置不正确。从数据库的角度看,它们是正确的,但从类的角度看,它们不会返回你想要的 您需要按如下方式设置您的类 客户端类
public Client()
{
Municipality = new Municipality();
Status = new ClientStatus();
}
[Key]
public System.Guid ClientIdentifier { get; set; }
public string CompanyName { get; set; }
public string SiteName { get; set; }
public string Consultant { get; set; }
public string Comments { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> Date { get; set; }
public string ClientInformation { get; set; }
public string ClientContactPerson { get; set; }
public string ClientEmail { get; set; }
public string ClientTel { get; set; }
public string ClientAddress { get; set; }
public string GPSCoordinates { get; set; }
public string Town { get; set; }
public string Suburb { get; set; }
public string CreatedBy { get; set; }
public Nullable<System.DateTime> DateCreated { get; set; }
public string LastEditedBy { get; set; }
public Nullable<System.DateTime> DateLastEdited { get; set; }
// Create Forign Key Connections
public Municipality Municipality { get; set; }
public ClientStatus Status { get; set; }
}
public class Municipality
{
public Municipality() {
MunicipalRegion = new MunicipalRegion();
}
[Key]
public System.Guid MunicipalityIdentifier {
get;
set;
}
public string MunicipalityName { get; set; }
public string CreatedBy { get; set; }
public Nullable<System.DateTime> DateCreated { get; set; }
public string LastEditedBy { get; set; }
public Nullable<System.DateTime> DateLastEdited { get; set; }
public MunicipalRegion MunicipalRegion { get; set; }
}
嗨,Shawn。抱歉,我的问题可能不够清楚,这是我表格的屏幕截图。如您所见,它显示市政区域和市政的唯一标识符。我需要显示市政区域和市政的名称,这些名称存储在各自的表中,并链接到客户表。
var data = clients.Select(cl => new
{
ClientIdentifier = cl.ClientIdentifier,
CompanyName = cl.CompanyName,
MunicipalRegion = cl.Municipality.MunicipalityName,
Status = cl.Status.ClientStatusName
});