Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.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# 无法在我的MVC项目中显示产品(课程)_C#_Asp.net Mvc - Fatal编程技术网

C# 无法在我的MVC项目中显示产品(课程)

C# 无法在我的MVC项目中显示产品(课程),c#,asp.net-mvc,C#,Asp.net Mvc,我的MVC项目需要帮助。 我的页面将显示可用的课程,所以我需要在页面上显示课程 我的代码看起来像这样,但不起作用 我尝试在中显示CourseView模型中课程的属性 public ActionResult Index() { var viewModel = new List<CourseViewModel>(); CourseViewModel Courses = new CourseViewModel(); Courses = new CourseVie

我的MVC项目需要帮助。 我的页面将显示可用的课程,所以我需要在页面上显示课程 我的代码看起来像这样,但不起作用 我尝试在中显示CourseView模型中课程的属性

public ActionResult Index()
{
    var viewModel = new List<CourseViewModel>();

    CourseViewModel Courses = new CourseViewModel();

    Courses = new CourseViewModel();

    return View(viewModel);
}

如果有人能帮助我,我将非常高兴。

根据您的评论,假设您的班级是这样的

public class CourseViewModel {
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public string CoursePlace { get; set; }
    public string CourseLevel { get; set; }
    public string CourseDate { get; set; }
    public string CourseDescription { get; set; }
    public string CourseBookUrl { get; set; }
}
填充列表以作为视图的viewmodel传递

public ActionResult Index() {
    var viewModel = new List<CourseViewModel>();

    //Either get the viewmodels from a data source or add them yourself

    var course = new CourseViewModel() {
        CourseId = 1,
        CourseName = "Maths 101",
        CoursePlace = "Some class room",
        CourseLevel = "1",
        CourseDate = "",
        //..populate other properties
    };

    viewModel.Add(course);

    //repeat to add more courses


    return View(viewModel);
}
在视图中,可以指定视图模型的类型

@model List<CourseViewModel>
您可以遍历模型并显示详细信息

@foreach (var course in Model) {
    <div>@course.CourseName</div>
    <!--other details you want to display-->
}

您似乎返回了一个空列表,并且没有对您创建的CourseViewModel执行任何操作,但是很难知道您应该执行什么操作。您遇到的错误是什么?正如@DStanley所提到的,您返回的是一个空的ViewModel。@garfbradaz,我的courseviewmodel看起来像公共虚拟列表Courses{get;set;}公共int CourseId{get;set;}公共字符串CourseName{get;set;}公共字符串CoursePlace{get;set;}公共字符串CourseLevel{get;set;}public string CourseDate{get;set;}public string CourseDescription{get;set;}public string CourseBookUrl{get;set;}我应该如何将actionresult与viewmodel连接以显示课程?@JPo:请用评论中的代码更新您的问题,这不容易阅读。@garfbradaz,我应该如何将我的viewmodel与actionresult连接起来,以便显示我的课程?