网格内的MVC下拉列表+代码优先迁移 [英] MVC dropdownlist inside a grid + Code First Migrations
问题描述
亲爱的编程社区,
我正在使用代码首次迁移,我试图找出如何从多对一关系中检索数据。换句话说,我有一名医生,其中一名医生可以属于许多病房,但病房一次只能有一名医生。
我的目标是找回医生''位于网格内的下拉列表。
请帮助我。欢迎提出任何意见或建议。
医生型号
< pre lang =c#> public class DoctorModel
{
[Key]
[显示(名称= 医生ID:)]
[必需(ErrorMessage = 需要医生ID!)]
public int Doctor_id { get ; set ; }
[显示(名称= 医生用户名:)]
[必需(ErrorMessage = 医生用户名是必需的!)]
[StringLength( 50 ,ErrorMessage = 必须在50个字符!)]
public string Doctor_username {获得跨度>; set ; }
[显示(名称= Doctor Bleep ID:)]
[必需(ErrorMessage = 需要Doctor Bleep ID!)]
public int Doctor_bleep_id { get ; set ; }
public string Image_name {获得跨度>; set ; }
public int Image_size {获得跨度>; set ; }
[显示(名称= 上传文件:)]
public byte [] Image_bytes { get 跨度>; set ; }
// 一对多关系
public virtual ICollection< EquipmentModel>设备{获取; set ; }
public virtual ICollection< WardModel> Wards { get ; set ; }
public int ? Gender_id { get ; set ; }
public virtual GenderModel Gender { get < /跨度>; set ; }
}
Ward Model
public class WardModel
{
[Key]
[显示(名称= Ward ID :)]
public int Ward_id {获得跨度>; set ; }
[显示(名称= 病房名称:)]
[必需(ErrorMessage = 病房名称是必需的!)]
[ StringLength( 50 ,ErrorMessage = 必须小于50个字符!)]
public string Ward_name {获得跨度>; set ; }
[显示(名称= 病房容量:)]
[必需(ErrorMessage = 需要Ward容量!)]
< span class =code-keyword> public int Ward_capacity { get ; set ; }
[显示(名称= Ward专业:)]
[必需(ErrorMessage = 需要Ward Specialty!)]
[ StringLength( 50 ,ErrorMessage = 必须小于50个字符!)]
public string Ward_speciality {获得跨度>; set ; }
[显示(名称= 病房状态:)]
public string Ward_status { get ; set ; }
[显示(名称= Ward Moto:)]
[StringLength( 50 ,ErrorMessage = 必须不超过50个字符!)]
public string Ward_moto {获得跨度>; set ; }
// 多对一关系
[显示( Name = Ward Manager:)]
public int ? Doctor_id { get ; set ; }
public virtual DoctorModel Doctor { get < /跨度>; set ; }
// 一对多关系
public virtual ICollection< PatientModel>患者{获取; set ; }
public virtual ICollection< NurseModel>护士{获取; set ; }
}
查看
@ {
ViewBag.Title = 指数 跨度>;
}
< link rel = stylesheet href = @ Url.Content(〜/ Content / CoreCSS / GridStyle.css ) />
< script src = @ Url.Content( 〜/ Content / Scripts / GenericDetailScripts.js) type = text / javascript > < / script >
@if( Model!= null )
{
< section class = contentContainer >
@ {
var grid = new WebGrid(Mo del,canPage: true ,rowsPerPage: 15 );
grid.Pager(WebGridPagerModes.NextPrevious);
@ grid.GetHtml(tableStyle: listing-border,headerStyle: k-header myGrid-space,footerStyle: k-pager-wrap k-grid-pager,rowStyle: td-dark,alternatingRowStyle: td-light,
htmlAttributes: new {id = DataTable },
列:grid.Columns(
grid.Column( Doctor_id , 医生ID:,样式: c olMin),
grid.Column(header: Wards,格式:@< text> @ Html.DropDownList( Wards)< / text > )
));
}
< / section >
}
在我的控制器中
//
// 获取:/医生/
[授权]
public ViewResult Index()
{
ViewBag.Wards = new SelectList(db.Wards, Ward_id, Ward_name);
return 查看(db.Doctors.ToList());
}
========== ================================================== =========================
更新
======== ================================================== ===========================
根据jerrykids的建议我更改了以下行
grid.Column(标题: < span class =code-string> Wards
,格式:@< text> @ Html.DropDownList( Wards)< / text > )
to
< pre lang =c#> @ Html.DropDownList( Wards,(SelectList)ViewBag.Wards )
输出的是现在我得到的所有w的列表网格中每位医生的ards。
但是,我有兴趣检索病房清单WHERE医生ID是item.Doctor_id。 - > basicaly检索特定网格行的doctor_id。尝试
@ Html.DropDownList(< span class =code-string> Wards,(SelectList)ViewBag.Wards)
Dear programming community,
I am using code first migrations and i am trying to figure out how i can retrieve data from "Many-to-one" relationships. In other words i have a Doctor where one doctor can belong to many wards but ward can have only one doctor at a time.
My objective is to retrieve doctors'' wards for a dropdownlist located inside a grid.
Please help me. Any comments or suggestions are welcome.
Doctor Model
public class DoctorModel
{
[Key]
[Display(Name = "Doctor ID:")]
[Required(ErrorMessage = "Doctor ID is required!")]
public int Doctor_id { get; set; }
[Display(Name = "Doctor User Name:")]
[Required(ErrorMessage = "Doctor User Name is required!")]
[StringLength(50, ErrorMessage = "Must be under 50 characters!")]
public string Doctor_username { get; set; }
[Display(Name = "Doctor Bleep ID:")]
[Required(ErrorMessage = "Doctor Bleep ID is required!")]
public int Doctor_bleep_id { get; set; }
public string Image_name { get; set; }
public int Image_size { get; set; }
[Display(Name = "Upload File:")]
public byte[] Image_bytes { get; set; }
// one to many relationships
public virtual ICollection<EquipmentModel> Equipments { get; set; }
public virtual ICollection<WardModel> Wards { get; set; }
public int? Gender_id { get; set; }
public virtual GenderModel Gender { get; set; }
}
Ward Model
public class WardModel
{
[Key]
[Display(Name = "Ward ID:")]
public int Ward_id { get; set; }
[Display(Name = "Ward Name:")]
[Required(ErrorMessage = "Ward Name is required!")]
[StringLength(50, ErrorMessage = "Must be under 50 characters!")]
public string Ward_name { get; set; }
[Display(Name = "Ward Capacity:")]
[Required(ErrorMessage = "Ward Capacity is required!")]
public int Ward_capacity { get; set; }
[Display(Name = "Ward Speciality:")]
[Required(ErrorMessage = "Ward Speciality is required!")]
[StringLength(50, ErrorMessage = "Must be under 50 characters!")]
public string Ward_speciality { get; set; }
[Display(Name = "Ward Status:")]
public string Ward_status { get; set; }
[Display(Name = "Ward Moto:")]
[StringLength(50, ErrorMessage = "Must be under 50 characters!")]
public string Ward_moto { get; set; }
// many to one relationships
[Display(Name = "Ward Manager:")]
public int? Doctor_id { get; set; }
public virtual DoctorModel Doctor { get; set; }
// one to many relationships
public virtual ICollection<PatientModel> Patients { get; set; }
public virtual ICollection<NurseModel> Nurses { get; set; }
}
View
@{
ViewBag.Title = "Index";
}
<link rel="stylesheet" href="@Url.Content("~/Content/CoreCSS/GridStyle.css")" />
<script src="@Url.Content("~/Content/Scripts/GenericDetailScripts.js")" type="text/javascript"></script>
@if (Model != null)
{
<section class="contentContainer">
@{
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 15);
grid.Pager(WebGridPagerModes.NextPrevious);
@grid.GetHtml(tableStyle: "listing-border", headerStyle: "k-header myGrid-space", footerStyle: "k-pager-wrap k-grid-pager", rowStyle: "td-dark", alternatingRowStyle: "td-light",
htmlAttributes: new { id = "DataTable" },
columns: grid.Columns(
grid.Column("Doctor_id", "Doctor ID:", style: "colMin"),
grid.Column(header: "Wards", format: @<text>@Html.DropDownList("Wards")</text>)
));
}
</section>
}
In my controller
//
// GET: /Doctor/
[Authorize]
public ViewResult Index()
{
ViewBag.Wards = new SelectList(db.Wards, "Ward_id", "Ward_name");
return View(db.Doctors.ToList());
}
=====================================================================================
Updated
=====================================================================================
Based on jerrykids'' suggestion I changed the following line
grid.Column(header: "Wards", format: @<text>@Html.DropDownList("Wards")</text>)
to
@Html.DropDownList("Wards", (SelectList)ViewBag.Wards)
The output of that is that now i am getting a list of all the wards for every doctor in the grid.
However, i am interested in retrieveing a list of wards WHERE id of the doctor is item.Doctor_id. -> basicaly retrieve the doctor_id for a particular grid row.
Try
@Html.DropDownList("Wards", (SelectList)ViewBag.Wards)
这篇关于网格内的MVC下拉列表+代码优先迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!