Asp.net mvc 视图中的多个强类型模型
我试图在MVC4中创建一个“新建”页面。该页面将有文本字段和下拉字段。我希望下拉框中列出的项目来自其他型号 主模型:(忽略语法,因为下面的代码是一个通用示例) someDetails模型:Asp.net mvc 视图中的多个强类型模型,asp.net-mvc,Asp.net Mvc,我试图在MVC4中创建一个“新建”页面。该页面将有文本字段和下拉字段。我希望下拉框中列出的项目来自其他型号 主模型:(忽略语法,因为下面的代码是一个通用示例) someDetails模型: public int detailID {get; set;} public int MainID {get; set;} ... public int actionID {get; set;} 行动模式: public int actionID {get; set;} public int locatio
public int detailID {get; set;}
public int MainID {get; set;}
...
public int actionID {get; set;}
行动模式:
public int actionID {get; set;}
public int location {get; set;}
public string action {get; set;}
SomeDetails
包含有关Main
的附加信息,并使用mainID
链接。我对为someDetails
创建“添加详细信息”页面感兴趣。此页面的一部分将包括一个基于main
中的位置
字段的操作
下拉列表
实现下拉框的最佳方式是什么?我有一个解决方案,涉及在ViewBag中传递可能的值,但我看到过这种被称为“坏习惯”的方法。我还认为可以将public int-actionID{get;set;}
更改为Action
类型。如果使用此方法,我想我会让someDetails
的构造函数填充列表?如果只是查看特定的someDetails
,这会不会导致性能问题,因为它每次都会尝试创建列表
创建操作:
public ActionResult Create(int id = -1)
{
if (id == -1)
{
//NOTE: INSERT REDIRECT HERE
}
someDetails model = new someDetails();
model.MainID = id;
return View(model);
}
编辑:
根据要求,更好地解释业务逻辑
我展示的主模型
是根据我无法更改的现有表建模的,因为它是我控制/组之外的数据仓库的一部分。我正在创建一个应用程序,允许用户添加someDetails
,以便更好地查看/跟踪这些数据。Action
模型仅根据位置显示可能的操作/actionID
我的应用程序将允许用户从
main
模型中搜索或过滤一组项目。然后允许他们添加someDetails
,这将显示一个表单,其中包含一些文本字段和一个下拉列表来选择操作。要获取操作的可能值
,我将执行类似于从操作a中选择*的操作,其中a.location=currentLocation
只需创建一个带有每个模型属性的ViewModel。。。可以在控制器中填充该ViewModel,然后将其传递给视图
编辑:对于下拉列表:
public int actionId { get; set;}
public SelectList ListOfActions { get; set;}
//Other model members
@Html.DropDownListFor(model => model.actionId, Model.ListOfActions, "Select one action", new { @class = "yourclass" })
var model = new YourViewModel();
model.ListOfActions = new SelectList(this.GetListOfActions(), "ActionName", "ActionId", [here the selected action id in case you need it])
在ViewModel中,所选操作id需要一个属性,列表本身需要另一个属性
在模型中:
public int actionId { get; set;}
public SelectList ListOfActions { get; set;}
//Other model members
@Html.DropDownListFor(model => model.actionId, Model.ListOfActions, "Select one action", new { @class = "yourclass" })
var model = new YourViewModel();
model.ListOfActions = new SelectList(this.GetListOfActions(), "ActionName", "ActionId", [here the selected action id in case you need it])
在视图中:
public int actionId { get; set;}
public SelectList ListOfActions { get; set;}
//Other model members
@Html.DropDownListFor(model => model.actionId, Model.ListOfActions, "Select one action", new { @class = "yourclass" })
var model = new YourViewModel();
model.ListOfActions = new SelectList(this.GetListOfActions(), "ActionName", "ActionId", [here the selected action id in case you need it])
在控制器中:
public int actionId { get; set;}
public SelectList ListOfActions { get; set;}
//Other model members
@Html.DropDownListFor(model => model.actionId, Model.ListOfActions, "Select one action", new { @class = "yourclass" })
var model = new YourViewModel();
model.ListOfActions = new SelectList(this.GetListOfActions(), "ActionName", "ActionId", [here the selected action id in case you need it])
对不起,在电话里。您是否考虑过使用viewmodels?创建一个复合viewmodel…然后您可以使用viewmodel.com中声明的SelectList。我完全忘记了MVC的这一方面。周末我的大脑一定是冬眠了。该死的星期一。感谢经常发生的事情:)我想接下来的问题是:我的列表的数据类型应该是什么?我需要保留
actionID
和action
的值。一本字典最适合这本书吗?@Jeff,我不太明白你想完成什么。如果你能用一些真实的模型(而不是虚拟模型)更新你的问题,并更好地解释业务逻辑。。。我们可以以适当的方式建议您将其映射到viewmodels中。我已经为业务逻辑添加了更多详细信息,但我无法粘贴整个表,因为数据是专有的,并且包含的列太多。