C# 尝试将多个模型添加到视图时出错
我需要在我的视图中有2个C# 尝试将多个模型添加到视图时出错,c#,asp.net-mvc,html-helper,C#,Asp.net Mvc,Html Helper,我需要在我的视图中有2个模型。但由于我们只能添加1个视图,因此我采用了以下方法: @model Tuple<My.Models.Mod1,My.Models.Mod2> @Html.DropDownListFor(m => m.Item2.humanKind,Model.Item2.allHuman) @Html.TextBoxFor(m => m.Item1.food) 这是什么?我如何解决这个问题 更新 public ActionResult
模型。但由于我们只能添加1个视图,因此我采用了以下方法:
@model Tuple<My.Models.Mod1,My.Models.Mod2>
@Html.DropDownListFor(m => m.Item2.humanKind,Model.Item2.allHuman)
@Html.TextBoxFor(m => m.Item1.food)
这是什么?我如何解决这个问题
更新
public ActionResult Index()
{
var model2 = new Mod2 { allHuman = allHumans() };
var model1 = new Mod1(); // JUST NOW I ADDED THIS, BUT IT DOESn't WORK
return View(model1,model2);
}
正在从只传入My.Models.Mod2
而不是元组的控制器操作调用该视图
仔细检查调用此视图的特定控制器操作
更新
您的控制器代码
return View(model1,model2);
应该是
return View(new Tuple<My.Models.Mod1,My.Models.Mod2>(model1, model2>);
返回视图(新元组(model1,model2>);
您正在将model1和model2作为单独的参数传递,而不是作为元组传递。所讨论的视图是从只传递My.Models.Mod2而不是元组的控制器操作调用的
仔细检查调用此视图的特定控制器操作
更新
您的控制器代码
return View(model1,model2);
应该是
return View(new Tuple<My.Models.Mod1,My.Models.Mod2>(model1, model2>);
返回视图(新元组(model1,model2>);
您将模型1和模型2作为单独的参数传递,而不是作为元组传递。在将元组实例发送到视图之前,您不会创建元组实例
public ActionResult Index()
{
var model2 = new Mod2 { allHuman = allHumans() };
var model1 = new Mod1();
return View(new Tuple<Mod1,Mod2>(model1,model2));
}
public ActionResult Index()
{
var model2=new Mod2{allHuman=allHumans()};
var model1=新的Mod1();
返回视图(新元组(model1,model2));
}
在将元组实例发送到视图之前,您没有创建元组实例
public ActionResult Index()
{
var model2 = new Mod2 { allHuman = allHumans() };
var model1 = new Mod1();
return View(new Tuple<Mod1,Mod2>(model1,model2));
}
public ActionResult Index()
{
var model2=new Mod2{allHuman=allHumans()};
var model1=新的Mod1();
返回视图(新元组(model1,model2));
}
每个视图只能有一个模型。您需要按照Ufuk的建议实例化元组
但是,我建议创建一个新模型,将其他模型作为属性。每个视图只能有一个模型。您需要按照Ufuk的建议实例化元组
但是,我建议创建一个新模型,将其他模型作为属性。构建一个同时包含以下两个属性的视图模型:
Public class CompositeViewModel{
Public Mod1 mod1 {get;set;}
Public Mod2 mod2 {get;set}
}
然后构造并将CompositeViewModel传递给view。将视图设置为使用CompositeViewModel作为模型@model CompositeViewModel
使用元组不容易让您扩展或更改正在执行的操作
甚至可能看起来您有一个包含数据的ViewModel,然后有一些关联的IEnumerable
。如果是这种情况,请将ViewModel命名为CreateAnimalTypeViewModel,其中包含创建它所需的所有属性,然后有各种选择列表
如果您需要从某个对象映射到ViewModel,例如,如果您正在编辑现有项目,则可以使用AutoMaper。构建一个包含以下内容的视图模型:
Public class CompositeViewModel{
Public Mod1 mod1 {get;set;}
Public Mod2 mod2 {get;set}
}
然后构造并将CompositeViewModel传递给view。将视图设置为使用CompositeViewModel作为模型@model CompositeViewModel
使用元组不容易让您扩展或更改正在执行的操作
甚至可能看起来您有一个包含数据的ViewModel,然后有一些关联的IEnumerable
。如果是这种情况,请将ViewModel命名为CreateAnimalTypeViewModel,其中包含创建它所需的所有属性,然后有各种选择列表
如果您需要从某个对象映射到ViewModel,例如,如果您正在编辑现有项目,您可以使用AutoMapper。发布控制器代码。您的答案在@model Tuple
代码行中。对不起@EricJ。我理解错了,您是对的。@UfukHacıoğulları我已经用codereturn视图(新Tuple)更新了我的帖子(model1,model2);发布您的控制器代码。您的答案在@model Tuple
代码行中。对不起@EricJ。我理解错了,您是对的。@UfukHacıoğulları我已经用codereturn视图更新了我的帖子(新元组(model1,model2);我同意尝试在中破解多个模型不是一个好主意,维护起来会很麻烦,最好研究使用renderpartial/renderaction。我同意尝试在中破解多个模型不是一个好主意,维护起来会很麻烦,最好研究使用renderpartial/renderaction