如何使用实体框架在mvc中显示下拉列表 [英] How to display a dropdown in mvc using entity framework

查看:83
本文介绍了如何使用实体框架在mvc中显示下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是mvc新手,我想填写并显示一个下拉列表。但问题是我有两个表命名团队和用户。插入的团队应该是显示为用户屏幕的下拉列表。我尝试了不同的类型,但徒劳无功...有人可以帮助我了吗



谢谢,

Maruthi Ram。



Hi,
I am new to mvc i want to fill and display a drop down list.But the problem is i am having two tables naming Team and Users.The team that is inserted should be displayed as a dropdown in users screen.I have tried different types but in vain...Can someone help me plzz

Thanks,
Maruthi Ram.

public class Team
   {
       public int TeamId { get; set; }
       public String TeamName { get; set; }
       public int TeamSize { get; set; }
   }







public class Users
   {

       public int UserId { get; set; }

       public int TeamId { get; set; }

       public String UserName { get; set; }

       public DateTime DateofBirth { get; set; }

       public bool ActiveYN { get; set; }

       public bool DeletedYN { get; set; }

       public bool DOBNotificationAccepted { get; set; }

       public DateTime CreatedOn { get; set; }

       public DateTime LastUpdatedOn { get; set; }

       public int CreatedBy { get; set; }

       public int LastUpdatedBy { get; set; }

   }





这两个类......



These are the two classes...

推荐答案

让我尝试...



一种方法是将所有对象作为模型传递。理想情况下使用ViewModel,它将包含所有属性的列表,然后将其绑定到您的视图。这是MVVM模式的实现。



否则,因为它看起来像你想要做的轻量级东西,只需通过控制器中的一个视图包发送一个选择列表。如...



查看:

Let me attempt...

One way is to to pass in all of your objects as the "model". Ideally use a ViewModel which will contain the list of all properties and then bind that to your view. This is implementation of the MVVM pattern.

Else, since it looks like a light-weight thing that you are trying to do, simply send a selectlist through a viewbag in controller. Such as...

View:
<label for="Team">Team</label>
   @Html.DropDownListFor("SelectedItem", ViewBag.ListOfTeams)





控制器:



Controller:

public ActionResult GetTeams()
{
    ViewBag.ListOfTeams = GetTeam();
    return View();
}


这篇关于如何使用实体框架在mvc中显示下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆