如何获得链接关系



我有基本模型

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

我viewModel

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

我想要查看下拉列表表组。我为它做了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();
}

最新更新