English 中文(简体)
ASP. NET MVC3, 为什么辍学者甚至以强烈的分类观点为依据。
原标题:ASP.NET MVC3, why does dropdownlist rely on viewbag even in a strongly typed view

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

我现在提出我的主计长和看法。 现在,当我看一下个人主计长时,我有以下工作:

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

而在《个人法典》中,《创建部门法》下降。

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

根据我的理解,DroopDownList Html的助手正在利用GisBag制作我的下级名单。 然而,第一民族和最后一个民族似乎在不依赖“观点”的情况下建立了自己的投入领域,因此,我认为,我本可以汇编时间,检查我的第一民族和最后一个民族领域,因为我的观点是严格分类的,而不是由我的德洛普·德南德斯。

是否有理由说明为什么对各省的DroopDownList没有大打字,或者我是否做过错,并且有办法使它严格分类?

Thanks :)

最佳回答

你们之所以需要一种观点,是因为你的看法约束的你的个人情况,没有所有可能的部门的数据,而只有属于哪个部门。 这行的情况:

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.

问题回答

如果你使用强烈的分类观点,那么你就应当有一种强烈的分类模式。

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

Hope this makes sense.





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签