I am trying to make a 'Create New' page in MVC 4. The page will have text fields and dropdown fields. I want the items listed in the dropdown box to come from another model.
我正在尝试在MVC 4中创建一个“创建新的”页面。页面将包含文本字段和下拉字段。我希望下拉框中列出的项目来自另一个模型。
Main Model: (ignore syntax as following code is a generic example)
主模型:(忽略语法,如下代码是一个通用示例)
public int MainID {get; set;}
public int location {get; set;}
...
someDetails Model:
someDetails模型:
public int detailID {get; set;}
public int MainID {get; set;}
...
public int actionID {get; set;}
Action Model:
操作模式:
public int actionID {get; set;}
public int location {get; set;}
public string action {get; set;}
SomeDetails
carries additional information about Main
and is linked by using mainID
. I am interested in creating a 'Add Details' pages for someDetails
. Part of this page will include a dropdown for Action
that is based on the location
field in main
.
一些细节包含有关Main的附加信息,并通过使用mainID进行链接。我有兴趣为某些细节创建一个“添加细节”页面。这个页面的一部分将包含一个基于main中的location字段的下拉菜单。
What would be the best way to implement the dropdown box? I have a solution that involves passing possible values in the ViewBag, but I've seen this labeled bad practice. I was also thinking that it might be possible to change public int actionID {get; set;}
to a type of <List>Action
. If doing this method, I think I would have the constructor for someDetails
populate the list? Would this cause performance issues when simply viewing a particular someDetails
since it would try to create the list each time?
实现下拉框的最佳方式是什么?我有一个在ViewBag中传递可能的值的解决方案,但是我已经看到了这个标记为糟糕的实践。我还在想也许可以改变公共int actionID {get;将}设置为
操作的类型。如果使用这个方法,我想我应该让someDetails的构造函数填充列表?这是否会导致性能问题,当仅仅查看特定的某些细节时,因为它将尝试每次创建列表?
Create action:
创建动作:
public ActionResult Create(int id = -1)
{
if (id == -1)
{
//NOTE: INSERT REDIRECT HERE
}
someDetails model = new someDetails();
model.MainID = id;
return View(model);
}
EDIT: As requested, better explanation of business logic The main model
I have shown is modeled after an existing table that I am not able to change as it is part of a data warehouse that lives outside my control/group. I am creating an application which allows users to add someDetails
in order to gain better visibility/tracking of some of this data. The Action
model simply displays possible actions/actionIDs based on the location.
编辑:根据请求,更好地解释业务逻辑,我所展示的主要模型是模仿现有的表,我无法更改,因为它是位于我的控制/组之外的数据仓库的一部分。我正在创建一个应用程序,它允许用户添加一些细节,以便更好地查看/跟踪这些数据。动作模型仅仅根据位置显示可能的动作/动作。
My application will allow a user to search for or filter a set of items from the main
model. They will be then allowed to add someDetails
which will bring up a form which will contain some text fields and a dropdown list to pick an action. To get possible values for action
, I would do something like SELECT * FROM Action a WHERE a.location = currentLocation
我的应用程序将允许用户从主模型中搜索或过滤一组项。他们将被允许添加一些细节,这些细节将包含一些文本字段和下拉列表来选择一个动作。为了获得可能的操作值,我将执行SELECT * FROM action a WHERE a。位置= currentLocation
1 个解决方案
#1
2
Just create a ViewModel with properties for each model... and that ViewModel can be populated in your controller and then passed to the view.
只需为每个模型创建一个带有属性的视图模型……这个ViewModel可以在控制器中填充,然后传递给视图。
EDIT: For DropDownLists:
编辑:dropdownlist:
In your ViewModel you need a property for the selected action id, and another to the list itself.
在您的ViewModel中,需要为所选的操作id和列表本身提供一个属性。
In the model:
在模型中:
public int actionId { get; set;}
public SelectList ListOfActions { get; set;}
//Other model members
In the View:
在视图:
@Html.DropDownListFor(model => model.actionId, Model.ListOfActions, "Select one action", new { @class = "yourclass" })
In your controller:
在你的控制器:
var model = new YourViewModel();
model.ListOfActions = new SelectList(this.GetListOfActions(), "ActionName", "ActionId", [here the selected action id in case you need it])
#1
2
Just create a ViewModel with properties for each model... and that ViewModel can be populated in your controller and then passed to the view.
只需为每个模型创建一个带有属性的视图模型……这个ViewModel可以在控制器中填充,然后传递给视图。
EDIT: For DropDownLists:
编辑:dropdownlist:
In your ViewModel you need a property for the selected action id, and another to the list itself.
在您的ViewModel中,需要为所选的操作id和列表本身提供一个属性。
In the model:
在模型中:
public int actionId { get; set;}
public SelectList ListOfActions { get; set;}
//Other model members
In the View:
在视图:
@Html.DropDownListFor(model => model.actionId, Model.ListOfActions, "Select one action", new { @class = "yourclass" })
In your controller:
在你的控制器:
var model = new YourViewModel();
model.ListOfActions = new SelectList(this.GetListOfActions(), "ActionName", "ActionId", [here the selected action id in case you need it])