ASP.NET MVC3,为什么dropdownlist即使在强类型视图中也依赖viewbag

发布于 2024-11-29 12:31:53 字数 1203 浏览 2 评论 0原文

我是 MVC 新手,所以也许这是一个愚蠢的问题 - 我正在尝试了解 ASP.NET MVC 中的强类型视图。我正在开发版本 3。 如果我有一个包含 2 个模型的项目 - 比如说人员和部门。一个人必须属于一个部门。所以我有我的部门模型(并且我已经生成了我的控制器和 CRUD 接口):

public class Department
{
    public int Id { get; set;}
    public string DeparmentName { get; set;}
}

然后我有一个引用部门的人员模型:

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public int Department_Id { get; set; }
    [ForeignKey("Department_Id")
    public virtual Department Department { get; set;}
}

现在我生成我的控制器和视图。现在,当我查看 PersonController 时,我有以下用于创建:

public ActionResult Create()
{
    ViewBag.Department_Id = new SelectList(db.Deparments, "Id", "DepartmentName");
    return View();
}

在 Person\Create.cshtml 中,创建部门下拉列表的代码是

@Html.DropDownList("Department_Id", String.Empty)

据我了解,DropDownList Html 帮助器正在使用 ViewBag 来创建我的下拉列表。但是,FirstName 和 LastName 似乎在不依赖 ViewBag 的情况下创建了输入字段 - 因此在我的视图中,我可以在编译时检查我的 FirstName 和 LastName 字段,因为我的视图是强类型的,但不是在我的 DropDownList 上。

部门的 DropDownList 没有强类型化是否有原因,或者我做错了什么并且有办法使其强类型化?

谢谢 :)

I'm new to MVC, so maybe this is a stupid question - I'm trying to get my head around strongly typed views in asp.net mvc. I'm working on version 3.
If I have a project with 2 models - say Person and Department. A person must belong to a department. So I have my Department model (and I've generated my controller and CRUD interface):

public class Department
{
    public int Id { get; set;}
    public string DeparmentName { get; set;}
}

Then I have a Person model which references Department:

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public int Department_Id { get; set; }
    [ForeignKey("Department_Id")
    public virtual Department Department { get; set;}
}

Now I generate my Controller and Views. Now, when I look into the PersonController, I have the following for the Create:

public ActionResult Create()
{
    ViewBag.Department_Id = new SelectList(db.Deparments, "Id", "DepartmentName");
    return View();
}

and in Person\Create.cshtml, the code to create the Department drop down is

@Html.DropDownList("Department_Id", String.Empty)

As I understand it, the DropDownList Html helper is using the ViewBag to create my drop down list. However, FirstName and LastName seem to have their input fields created without relying on the ViewBag - so in my View, I can have compile time checking on my FirstName and LastName fields because my view is strongly typed, but not on my DropDownList.

Is there a reason why the DropDownList for department not strongly typed or am I doing something wrong and there is a way to make it strongly typed?

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

_蜘蛛 2024-12-06 12:31:53

您需要 viewbag 的原因是因为您的视图绑定到的 Person 实例没有所有可能部门的数据,只有它所属的一个部门的数据。这一行发生了什么:

ViewBag.Department_Id = new SelectList(db.Deparments, "Id", "DepartmentName");

数据库中的所有部门都被添加到选择列表(供您的下拉菜单绑定到),其中 Id 作为键,DepartmentName 作为值。将您的视图绑定到人员后,下拉列表将显示相应的部门。

The reason you need a viewbag is because your Person instance, to which your view binds, does not have the data for all possible departments, only the one department it belongs to. What happens in this line:

ViewBag.Department_Id = new SelectList(db.Deparments, "Id", "DepartmentName");

Is that all Departments from the DB are added to the Select List (for your DropDown to bind to) with Id as key asn DepartmentName as value. After your view is bound to the Person, the dropdown will show the appropriate Department.

零度℉ 2024-12-06 12:31:53

如果您使用强类型视图,则应该有一个传递给视图的强类型模型:

public class ViewData {
   public IList<Departments> Departments {get; set;}
}

然后在控制器中您有:

public ActionResult Create()
{
    ViewData model = new ViewData();
    model.Departments = db.Deparments;
    return View(model);
 }

最后一步是创建选择列表并在视图上下拉:

@model ViewData
@SelectList list = new SelectList(Model.Deparments, "Id", "DepartmentName");
@Html.DropDownList("Department_Id", list);

希望这是有意义的。

If you are using strongly typed views, you should have a strongly typed model that you pass to the view:

public class ViewData {
   public IList<Departments> Departments {get; set;}
}

Then in you controller you have:

public ActionResult Create()
{
    ViewData model = new ViewData();
    model.Departments = db.Deparments;
    return View(model);
 }

The last step is to create the select list and drop down on the view:

@model ViewData
@SelectList list = new SelectList(Model.Deparments, "Id", "DepartmentName");
@Html.DropDownList("Department_Id", list);

Hope this makes sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文