Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
676 views
in Technique[技术] by (71.8m points)

c# - Partial view Menu - Send info to view

That's how I try to get a view that includes a class with. The way I get it on it is by writing:

@Html.Partial("View/ViewBar_Info/Menu")

See how my view/controlle looks out

If I'm doing an ordinary MVC view, I can do that. but that's because I have to get the menu that is being controlled via the database. Therefore, I try to do this here.

How can I call that view?

Error here

Error Text:

An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately.

Generated Code One or more compilation references are missing. Ensure that your project is referencing 'Microsoft.NET.Sdk.Web' and the 'PreserveCompilationContext' property is not set to false.

Update EIDT:

I make this from _Layout.cshtml

@Html.Partial("View/ViewBar_Info/Menu.cshtml")

in the Menu.cshtml:

@page
@model NewWebsite_SITE_2018.Pages.View.ViewBar_Info.MenuModel

@foreach (var item in Model.GetListMenu)
{
    <a>@item.Name</a>
}

Error now its:

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'NewWebsite_SITE_2018.Pages.IndexModel', but this ViewDataDictionary instance requires a model item of type 'NewWebsite_SITE_2018.Pages.View.ViewBar_Info.MenuModel'.

What I'm trying to do is in my layout, so I'll call my menu so come on the single page.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First problem has been in Razor precompilation issue that fixed in 2.0.3 https://github.com/dotnet/core-setup/issues/2981

Second

When you call your partical you should send correct model to it if you not send razor try to send current model of view

send existed model

@Html.Partial("View/ViewBar_Info/Menu.cshtml", yourMenuModel)

or create new

@Html.Partial("View/ViewBar_Info/Menu.cshtml", new NewWebsite_SITE_2018.Pages.View.ViewBar_Info.MenuModel())

or modify your view for non required model

@{
var menuModel = new NewWebsite_SITE_2018.Pages.View.ViewBar_Info.MenuModel();
}

@foreach (var item in menuModel.GetListMenu)
{
    <a>@item.Name</a>
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...