【问题标题】:How to get a linked relationship如何获得关联关系
【发布时间】:2021-10-07 13:14:30
【问题描述】:

我有基础模型

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 Group。 我为它做了 ViewModel。但我不知道该怎么做? 我想我应该在 ViewModel 使用属性 Get 中得到它

public IEnumerable<SelectListItem> GroupDictionary { get; set; }

【问题讨论】:

    标签: c# model-view-controller asp.net-mvc-5


    【解决方案1】:

    我想得到什么:

    查看

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-12
      • 2017-03-30
      • 2020-01-15
      • 2021-03-10
      • 2014-07-22
      • 1970-01-01
      • 2014-10-23
      相关资源
      最近更新 更多