I’m trying to use multiple models in a single cshtml file. The code I have is:
@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>
What is wrong with what I’m doing above, and more importantly, how can I solve my issue? Thank you.
Best bet is to make a view model that has everything you need in it. Since your code is sparse, I’m going to take a shot in the dark:
Then change your first
@modelline to:@model CostumeContest.Models.MyViewModelObviously, you’d change the name of the class to something that makes sense for you. You would then create a
MyViewModelinstance, set theVoteModelandVoteResultproperties with it, and send the instance of theMyViewModelto the view, not aVoteModel.