我正在使用 Asp.net mvc 中的脚手架,
我在国家/地区的视图模型上有一个属性
public IEnumerable<SelectListItem> Countries { get; set; }
,但是,当我创建视图并指定视图模型时,它并没有像我预期的那样搭建下拉列表。事实上它被完全忽略了。
我在执行此操作之前已经编译了该项目,
我还尝试添加这样的属性
public int CountryId { get; set; }
正如本文建议的那样,工作中有一个约定
http://blog.stevensanderson.com/2011/01/28/mvcscaffolding-one-to-many-relationships/
我正在使用添加视图选项当右键单击控制器操作方法时,您有
什么想法是错误的吗?
I am playing with the Scaffolding that is in Asp.net mvc
I have a property on my view model for countries
public IEnumerable<SelectListItem> Countries { get; set; }
Yet, when I create a view and specify the viewmodel it doesn't scaffold a drop down list as I expected it to. In fact it get completely ignored.
I have compiled the project before doing it
I also tried adding a property like this
public int CountryId { get; set; }
As this article suggested there is a convention at work
http://blog.stevensanderson.com/2011/01/28/mvcscaffolding-one-to-many-relationships/
I am using the Add view option you have when right clicking in a controller action method
Any ideas what is wrong?
发布评论
评论(3)
在我当前的项目中,我遇到了这个问题,并且无法找到一种快速方法来构建我的实体之一内一对多关系的下拉列表。
我最终做的事情如下:
1-创建正常的 AddView=>Create 方式。
2- 如果我的模型类中有一个 ID 属性,则默认模板将生成类似这样的内容,以在我的视图中表示该属性:
3- 现在我需要用一个工作模板替换这个默认模板,所以我写了这个
CREATE
方法中的代码:这将获取 Cities 表并创建一个 Selectlist,我可以将其传递到我的视图并使用它来为 DrobDown 提供其项目。
4-将视图中的默认代码替换为如下代码:
我使用
ViewData["CityList"]
而不是ViewBag.CityList
的原因是这个有效,但另一个无效。5-现在我的视图正在工作查找,并且正在获取
City
数据,就像我所期望的那样,并且在我的Edit
视图中使用相同的模型也产生了一个工作视图。尝试一下,让我知道发生了什么,谢谢。
In my current project, i faced this problem and couldn't find a quick way to scaffold the Dropdown list of a one-many relation inside one of my Entities.
What I ended up doing was like the following:
1- Create the normal AddView=>Create way.
2- If i had a ID property in my model class, the defaul;t template will generate something like this to represent this property in my view:
3- Now I need to replace this default template with a working one, so I wrote this code in the
CREATE
method:this will fetch the Cities table and create a Selectlist that i can pass to my view and work with it to provide the DrobDown with it's items.
4- replace the default code in my View by one like the following:
The reason I've used
ViewData["CityList"]
instead ofViewBag.CityList
is that this one worked but the other not.5- now my view is working find and is fetching the
City
data just like what I expected, and using the same model inside myEdit
view resulted in a working one too.Give it a try and let me know what happened, Thanks.
我注意到,对于给定的模型,创建新控制器时“创建”脚手架生成的代码与右键单击现有控制器并说“添加视图”并选择“创建”脚手架模板不同。在第一种情况下,假设您在子类上具有正确的属性
,那么这种情况(添加具有 MVC 脚手架读/写和适当模型类的控制器)将为父关系生成一个 @Html.DropDownList,而在控制器 Create 方法不会搭建下拉列表,而是为关系创建一个 @Html.EditorFor 。
因此,如果您希望脚手架代码生成下拉列表,答案可能是删除并重新创建控制器(如果可能),否则手动添加适当的代码。
I have noticed that for a given model, the "Create" scaffold-generated code when creating a new controller is different than if you right-click in an existing controller and say "Add View" and choose the "Create" scaffolding template. In the first case, given you have the correct properties on the child class
then this case (adding controller with MVC scaffolding read/write and appropriate Model class) WILL generate a @Html.DropDownList for the parent relationship, whereas right-clicking within the controller Create method will not scaffold the drop-down but will instead create an @Html.EditorFor for the relationship.
So the answer if you want scaffolding code to generate the drop-down may be to delete and re-create your controller if possible, otherwise manually add in the appropriate code.
为了能够通过下拉列表选择国家/地区,模型中的属性应为:
public Country Country{ get;放; }
EF 使用的导航属性,不涉及数据库,public Country CountryId{ get;放; }
在 Person 表上创建外键
人员的每个实例/记录都与一个国家/地区关联:该关系是通过代码使用导航属性和数据库的 CountryID 定义的。
然后,脚手架模板将使用以下命令生成编辑/创建方法和视图:
这是一个类似的场景。
In order to have the option to choose a country with a dropdown the property in your Model should be:
public Country Country{ get; set; }
Navigation property used by EF, doesn't involve the databasewith
public Country CountryId{ get; set; }
Create the foreign key on the Person table
Each instance/record of a person is associated with a country: the relation is defined with the navigation property via code and with the CountryID for the database.
The scaffholding template will then generate the edit/create methods and views using :
Here's a similar scenario.