如何获得链接关系
How to get a linked relationship
我有基础模型
public class Group
{
public int Id { get; set; }
public int GroupVag { get; set; }
public ICollection<Vagon> Vagons { get; set; }
}
public class Vagon
{
public int Id { get; set; }
public string Nom_Vag { get; set; }
//[Required]
public int NumberGroup { get; set; }
[ForeignKey("Group")]
public int? GroupId { get; set; }
public Group Group { get; set; }
public virtual ICollection<SPR4664> SPR4664s { get; set; }
}
我的视图模型
public class ViewModelAddVag
{
public int Id { get; set; }
[Display(Name = "Name vag")]
[Required]
[MaxLength(8, ErrorMessage = "incorrect")]
public string Nom_Vag { get; set; }
[Display(Name = "Group vag")]
//[Required]
public int NumberGroup { get; set; }
[Display(Name = "TrainingType")]
[UIHint("DropDownList")]
public IEnumerable<SelectListItem> GroupDictionary { get; set; }
}
我想加入 View DropDownList table 组。
我为此做了 ViewModel。但我不知道该怎么做?
我想我应该在 ViewModel 中使用 属性 Get
获取它
public IEnumerable<SelectListItem> GroupDictionary { get; set; }
我想得到的:
查看
<div class="form-group">
<div class="col-md-10">
Grops <br />
@Html.DropDownListFor(model => model.Id, ViewBag.Group as SelectList, null, new { @class = "btn btn-info dropdown-toggle" })
<select name="TrainingTypeId" class="btn btn-light dropdown-toggle dropdown-toggle-split" aria-label="Default select example">
@foreach (var item in ViewBag.Group)
{
<option value="@item.Value">
@item.Text
</option>
}
</select>
</div>
</div>
控制器:
[HttpGet]
public ActionResult Create()
{
SelectList groupsList = new SelectList(db.groups, "Id", "GroupVag");
ViewBag.Group = groupsList;
return View();
}
我有基础模型
public class Group
{
public int Id { get; set; }
public int GroupVag { get; set; }
public ICollection<Vagon> Vagons { get; set; }
}
public class Vagon
{
public int Id { get; set; }
public string Nom_Vag { get; set; }
//[Required]
public int NumberGroup { get; set; }
[ForeignKey("Group")]
public int? GroupId { get; set; }
public Group Group { get; set; }
public virtual ICollection<SPR4664> SPR4664s { get; set; }
}
我的视图模型
public class ViewModelAddVag
{
public int Id { get; set; }
[Display(Name = "Name vag")]
[Required]
[MaxLength(8, ErrorMessage = "incorrect")]
public string Nom_Vag { get; set; }
[Display(Name = "Group vag")]
//[Required]
public int NumberGroup { get; set; }
[Display(Name = "TrainingType")]
[UIHint("DropDownList")]
public IEnumerable<SelectListItem> GroupDictionary { get; set; }
}
我想加入 View DropDownList table 组。 我为此做了 ViewModel。但我不知道该怎么做? 我想我应该在 ViewModel 中使用 属性 Get
获取它public IEnumerable<SelectListItem> GroupDictionary { get; set; }
我想得到的:
查看
<div class="form-group">
<div class="col-md-10">
Grops <br />
@Html.DropDownListFor(model => model.Id, ViewBag.Group as SelectList, null, new { @class = "btn btn-info dropdown-toggle" })
<select name="TrainingTypeId" class="btn btn-light dropdown-toggle dropdown-toggle-split" aria-label="Default select example">
@foreach (var item in ViewBag.Group)
{
<option value="@item.Value">
@item.Text
</option>
}
</select>
</div>
</div>
控制器:
[HttpGet]
public ActionResult Create()
{
SelectList groupsList = new SelectList(db.groups, "Id", "GroupVag");
ViewBag.Group = groupsList;
return View();
}