Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# html中的多个模型_C#_Html_Asp.net Mvc_Model - Fatal编程技术网

C# html中的多个模型

C# html中的多个模型,c#,html,asp.net-mvc,model,C#,Html,Asp.net Mvc,Model,我试图在一个cshtml文件中使用多个模型。我的密码是: @model CostumeContest.Models.VoteModel @{ ViewBag.Title = "Vote"; } <h2 style="color: #B45F04">Vote Receipt</h2> <b>Your Name:</b> @Model.VoterName <br /> <b>Your Vote for Best

我试图在一个cshtml文件中使用多个模型。我的密码是:

@model CostumeContest.Models.VoteModel  
@{
    ViewBag.Title = "Vote";
}


<h2 style="color: #B45F04">Vote Receipt</h2>
<b>Your Name:</b> @Model.VoterName
<br />
<b>Your Vote for Best Costume: </b>@Model.BestName
<br />
<b>Your Vote for Most Creative Costume:</b> @Model.CreativeName
<br />

<h2 style="color: #B45F04">Vote Summary</h2>

//@model CostumeContest.Models.VoteResult    this is giving me problems when not commented out
<h3 style="color: #B45F04">Best Costume</h3>


<h3 style="color: #B45F04">Most Creative Costume</h3> 
@model.Models.VoteModel
@{
ViewBag.Title=“投票”;
}
选票收据
您的姓名:@Model.VoterName

您对最佳服装的投票:@Model.BestName
您对最具创意服装的投票:@Model.CreativeName
投票摘要 //@model.Models.VoteResult这是给我的问题时,没有评论出来 最佳服装奖 最具创意的服装

我上面所做的有什么问题,更重要的是,我如何解决我的问题?谢谢。

您应该创建一个外部模型类,其中属性包含其他两个类。

最好的方法是创建一个包含所有所需内容的视图模型。由于您的代码很稀疏,我将在黑暗中尝试:

namespace CostumeContest.Models
{
    public class MyViewModel
    {
        public VoteModel VoteModel { get; set; }
        public VoteResult VoteResult { get; set; }
    }
}
然后将第一行
@model
更改为:

@model.Models.MyViewModel


显然,您应该将类的名称更改为对您有意义的名称。然后创建一个
MyViewModel
实例,设置
VoteModel
VoteResult
属性,并将
MyViewModel
实例发送到视图,而不是
VoteModel

使用
元组来保存这两个对象

@model Tuple<VoteModel, VoteResult>
@模型元组

格罗默的答案是更好的设计,但这是一个简单的解决方案。

这没有任何意义。你的行为会是什么样子?这正是我想要的。谢谢格罗默