ViewModel在View中查看的多个模型(Multiple Models in View by ViewModel)
我是ASP.NET MVC的初学者,并试图通过使用
ViewModel.cs
在视图中添加多个模型,但我无法在我的视图中得到它Index.cshtml
任何人都可以指导我如何获得它?ViewModel.cs
public class ViewModel { public IEnumerable<ContactLens.Models.manufacturer> manufacturer {get; set;} public IEnumerable<ContactLens.Models.brand> brand { get; set; } }
HomeController.cs
public class HomeController : Controller { private Entities db = new Entities(); public ActionResult Index() { ViewBag.Message = "Welcome to my demo!"; ViewModel mymodel = new ViewModel(); mymodel.manufacturer = db.manufacturers.ToList(); mymodel.brand = db.brands.ToList(); return View(mymodel); } }
Index.cshtml
@model ViewModel @{ ViewBag.Title = "Home Page"; } -------- <h3 class="menu_head">Top Manufacturers</h3> <ul class="menu"> @foreach (var manufacturer in Model) { <li class="item1"> <a href="#"><img class="arrow-img" src="images/f_menu.png" alt="" /> @manufacturer.man_name </a> </li> } </ul> <h3 class="menu_head">Top Brands</h3> <ul class="menu"> @foreach (var brand in Model) { <li class="item1"> <a href="#"><img class="arrow-img" src="images/f_menu.png" alt="" /> @brand.brand_name </a> </li> } </ul> -------
由于它在
@model ViewModel
显示错误,我无法访问Index.cshtml
模型。I am beginner in ASP.NET MVC and trying to add multiple models in view by using
ViewModel.cs
but I can't get it in my viewIndex.cshtml
anyone can guide me how can I get it?ViewModel.cs
public class ViewModel { public IEnumerable<ContactLens.Models.manufacturer> manufacturer {get; set;} public IEnumerable<ContactLens.Models.brand> brand { get; set; } }
HomeController.cs
public class HomeController : Controller { private Entities db = new Entities(); public ActionResult Index() { ViewBag.Message = "Welcome to my demo!"; ViewModel mymodel = new ViewModel(); mymodel.manufacturer = db.manufacturers.ToList(); mymodel.brand = db.brands.ToList(); return View(mymodel); } }
Index.cshtml
@model ViewModel @{ ViewBag.Title = "Home Page"; } -------- <h3 class="menu_head">Top Manufacturers</h3> <ul class="menu"> @foreach (var manufacturer in Model) { <li class="item1"> <a href="#"><img class="arrow-img" src="images/f_menu.png" alt="" /> @manufacturer.man_name </a> </li> } </ul> <h3 class="menu_head">Top Brands</h3> <ul class="menu"> @foreach (var brand in Model) { <li class="item1"> <a href="#"><img class="arrow-img" src="images/f_menu.png" alt="" /> @brand.brand_name </a> </li> } </ul> -------
It is showing me an error at
@model ViewModel
due to it, I can't access models inIndex.cshtml
.
原文:https://stackoverflow.com/questions/44452243
最满意答案
你应该有这样的结构:
projectRoot app build.gradle library build.gradle build.gradle settings.gradle
每个模块都有自己的
build.gradle
文件。root/settings.gradle
定义了项目中的所有模块。 不要在模块内使用其他settings.gradle。在settings.gradle中 :
include ':app', ':library'
在build.gradle中
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.2.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } }
在library / build.gradle中
apply plugin: 'com.android.library' android { compileSdkVersion ... buildToolsVersion ... defaultConfig { .... } } dependencies { //.... }
在app / build.gradle中使用您的文件,但更改:
dependencies { //... // GuillotineMenu compile project(":library") }
更新:
在您的评论如下,我认为在您的情况下,库文件夹是另一个项目的根文件夹。 这意味着您应该参考此项目中的模块 。
projectRoot app build.gradle libs guillotinemenu build.gradle //top level file of guillotinemenu project -->> module build.gradle //build.gradle of the module
因此,更改projectRoot的settings.gradle。
include ':app', ':guillotinemenu' project(':guillotinemenu').projectDir = new File('libs/guillotinemenu/module')
模块(libs / guillotinemenu / module)应该有一个
build.gradle
作为上面描述的library / build.gradle 。You should have a structure like this:
projectRoot app build.gradle library build.gradle build.gradle settings.gradle
Each module has own
build.gradle
file. Also theroot/settings.gradle
defines all modules inside a project. Don't use another settings.gradle inside your module.In settings.gradle :
include ':app', ':library'
In build.gradle
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.2.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } }
In library/build.gradle
apply plugin: 'com.android.library' android { compileSdkVersion ... buildToolsVersion ... defaultConfig { .... } } dependencies { //.... }
In app/build.gradle use your file but change:
dependencies { //... // GuillotineMenu compile project(":library") }
UPDATE:
After your comment below, I think that in your case the library folder is the root folder of another project. It means that you should refer to the module inside this project.
projectRoot app build.gradle libs guillotinemenu build.gradle //top level file of guillotinemenu project -->> module build.gradle //build.gradle of the module
So change the settings.gradle of your projectRoot.
include ':app', ':guillotinemenu' project(':guillotinemenu').projectDir = new File('libs/guillotinemenu/module')
The module (libs/guillotinemenu/module) should have a
build.gradle
as the library/build.gradle described above.
相关问答
更多-
错误:在Android Studio中找不到名称为“default”的配置(Error:Configuration with name 'default' not found in Android Studio)[2021-07-27]
你的问题在这里: include ':app' include 'app:libraries' //REMOVE THIS LINE! include 'app:libraries:tess-two' 由于您要定义include 'app:libraries' ,因此Gradle期望app / libraries文件夹中的build.gradle文件。 由于gradle未找到此文件,或者如果在build.gradle文件内,Gradle未找到默认配置,则会发生名称为“ default”的配置。 Your ... -
错误:在Android Studio中找不到名称为“default”的配置(Error: Configuration with name 'default' not found in Android Studio)[2023-05-15]
将您的库文件夹添加到项目的根目录中,然后复制所有的库文件。 对于exProject / library,然后同步它,休息的东西似乎对我来说很好。 Add your library folder in your root location of your project and copy all the library files there. For ex YourProject/library then sync it and rest things seems OK to me. -
这是项目settings.gradle文件项目dir位置的一个问题,它应该正确地指向项目构建目录,例如, project(':react-native-gmaps').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-gmaps/android/app') This is a problem with the project settings.gradle file project dir locatio ...
-
解决方案 该问题最终由Android Studio中的代理设置解决。 这些可以在“首选项”窗口中找到,它看起来像这样。 这篇文章也作为解决方案提供。 包含JAR文件 我发现我必须手动下载目标文件并将其添加到android studio,但我不确切知道我必须下载哪个文件? 该错误告诉您下载com.android.databinding:dataBinder (任何版本) 从哪里来? 这是JCenter的最新版本。 只需使用页面中间右侧的“文件”链接下载JAR文件即可。 如何添加到Android工作室? 因为你 ...
-
Gradle sync错误:找不到名称为“default”的配置(Gradle sync Error:Configuration with name 'default' not found)[2022-10-13]
你应该有这样的结构: projectRoot app build.gradle library build.gradle build.gradle settings.gradle 每个模块都有自己的build.gradle文件。 root/settings.gradle定义了项目中的所有模块。 不要在模块内使用其他settings.gradle。 在settings.gradle中 : include ':app', ':library' 在build.gradle中 ... -
如果您需要强制开发人员设置properties ,则应使用中止Gradle构建 throw new GradleException("your warning message") 如果您只想在concole中打印警告, gradle logger确实支持多个级别的日志记录。 例如 logger.warn('A warning log message.') If you need to force the developer to set your properties, you should abort ...
-
从你的错误日志看起来像你得到这个错误,因为 错误:找不到样式属性'attr / roundSelector(aka com.example.adend.timetable:attr / roundSelector)。 所以请检查一下 @绘制/ round_selector_dark 你有没有这个round_selector_dark你的drawable。 有多个错误,所有都与你的style.xml有关 所以仔细检查你的style.xml元素。 From Your Error Log seems Like y ...
-
从Gradle 3.0 CopySpec文档开始 , CopySpec不包含includes方法。 你应该使用include代替: task updateAbstracts(type: Sync) { group 'build' from '../../base' include '../../base/shared/**/*_abstract.*' } As of Gradle 3.0 CopySpec documentation, CopySpec does not conta ...
-
添加滑动菜单错误:找不到名称为“default”的配置(Adding Sliding menu Error:Configuration with name 'default' not found)[2021-09-17]
在android studio项目中导入模块的简单步骤。 第1步:转到文件>新建>导入模块 第2步:选择源目录 第3步:如果需要,重命名模块名称, 第4步:在build.gradle文件中添加模块。 Simple steps to import module in android studio project. Step 1: Goto File>New>Import Module Step 2: Select Source Directory Step 3: Rename Module Name if y ... -
Gradle同步失败(Gradle sync failed)[2022-06-01]
通过更新项目中的每个组件来解决它。 它现在不会抛出该错误。 Solved it by updating every single component in my project. It now does not throw that error.