ASP.NET MVC3,为什么DropDownList的依靠viewbag即使在强类型视图 [英] ASP.NET MVC3, why does dropdownlist rely on viewbag even in a strongly typed view

查看:174
本文介绍了ASP.NET MVC3,为什么DropDownList的依靠viewbag即使在强类型视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的MVC,也许这是一个愚蠢的问题 - 我试图让我的头围绕在asp.net mvc的强类型的意见。我工作的第3版。
如果我有2个型号项目 - 说人与部门。一个人必须属于一个部门。所以,我有我部模型(我已经生成了我的控制器和CRUD接口):

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

然后,我有一个Person模型,引用部:

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

现在我产生我的控制器和视图。现在,当我直视PersonController,我有以下为创建:

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

和在人\\ Create.cshtml中,code创建部下拉为

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

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

据我了解,DropDownList的HTML辅助使用ViewBag创建我的下拉列表中。然而,名字和姓氏似乎有自己的输入字段不依靠ViewBag创建 - 所以在我看来,我可以有编译时检查我的名字和姓氏字段,因为我的看法是强类型,却没有关于我的DropDownList

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.

时有一个原因的部门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?

感谢:)

推荐答案

您需要一个viewbag的原因是因为你的Person实例,对此您的看法绑定,没有对所有可能的部门的数据,仅一个部门它属于。在此行中会发生什么:

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");

时,从数据库的所有部门都加入到选择列表(你的下拉绑定到)用id作为关键ASN 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.

这篇关于ASP.NET MVC3,为什么DropDownList的依靠viewbag即使在强类型视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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