更改电影列表 MVC 教程 - 添加新的部分视图

发布于 2024-11-15 17:17:42 字数 1128 浏览 3 评论 0原文

我正在按照本教程学习基本的 MVC MVC 中的电影列表教程 我已经读完教程了。现在我想对本教程进行自己的修改。首先,我想向该电影列表视图添加一个新的部分视图。因此,在 DIV 中,我将以下代码放入已呈现电影列表的索引页中。

 @{Html.RenderPartial("_NewPartialView");}

为此,我首先在 Models 文件夹中为 NewClass.cs 创建一个新模型类。模型文件夹已经有一个Movie Model Class.Movie.cs 问题 1:我应该为新的局部视图创建一个单独的 Model .cs 文件,还是应该在同一个 Movie.cs 文件中创建其他类?

然后我使用基于新模型的脚手架创建了一个部分视图(现在我在单独的 .cs 文件中拥有新模型 - 这意味着我现在有两个模型,一个用于电影,一个用于 NewModel(用于部分视图)。我想要这个在原始电影列表索引页面中呈现为侧面的部分视图,

我有一个 MoviesController,其中包含以下代码

 public ViewResult Index()
    {
        return View(db.Movies.ToList());
    }

:“我还缺少其他内容吗?”如果我尝试运行此代码,它会破坏代码行

 @{Html.RenderPartial("_NewPartialView");}

并显示错误消息 I。此时的代码是“传递到字典中的模型项的类型为 System.Collections.Generic.List1[Project.Models.Movie]',但此字典需要类型为“System.Collections.Generic.IEnumerable1[Project.Models.NewModel]'。

在将基于新模型的新部分视图添加到现有视图/模型的步骤中,我需要一些 MVC 专家指导,我需要帮助并欣赏任何信息。将要帮助我学习。

I am following this tutorial to learn basic MVC Movie List Tutorial in MVC I am at the end of the tutorial. Now I want to do my own modification to this tutorial. First, I want to add a new partial view to this movie list view. So in a DIV, I put the following code in the index page that already renders the movie list.

 @{Html.RenderPartial("_NewPartialView");}

For this to work, first in the Models folder, I create a new model class for NewClass.cs. The model folder already has a Movie Model Class.Movie.cs
Question1 : Should I be creating a separate Model .cs file for the new partial view or Should I create additional classes in the same Movie.cs file?

Then I created a partial view using scaffolding based on the new Model (for now I have the new model in a separate .cs file - meaning I now have two Models one for Movies and one for the NewModel (for partialview). I want this rendered in the original movie list index page as a partial view on the side.

I have a MoviesController that has the code

 public ViewResult Index()
    {
        return View(db.Movies.ToList());
    }

Am I missing anything else? If I try to run this, it is breaking at the line of code

 @{Html.RenderPartial("_NewPartialView");}

and the error message I get at this point of the code is "The model item passed into the dictionary is of type System.Collections.Generic.List1[Project.Models.Movie]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[Project.Models.NewModel]'.

I need some MVC expert guidance in this step of adding a new partial view based on a new model to an existing View/Model. I need help and appreciate any tid bits of information that will help me learn. Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離人涙 2024-11-22 17:17:42

查看您的错误消息,您的视图需要一个 IEnumerable ,其中包含 Project.Models.NewModel 的类,而您传递的 List 包含以下类项目.模型.电影。基本上,您传递的视图数据不是为处理而设计的。您可以更改顶部的视图/部分以读取类似内容:

@model List<Project.Models.Movie>

这将您的视图设置为期望一个动作列表,这就是示例中从控制器传递视图的内容。顺便说一句

@model IEnumerable<Project.Models.Movie>

List 继承自 IEnumerable 也可以正常工作。

如果您希望视图接受 IEnumerable 那么您需要将控制器修改为类似以下内容:(我在这里对您的模型属性进行假设)

public ViewResult Index()
{
    return View(db.Movies.Select(m => new NewModel { Title = m.Title, Director = m.Director}));
}

然后这将通过一个 IEnumerable 到您的视图。

另一方面,在 razor 中引用部分的更简单方法是:

@Html.Partial("_NewPartialView")

更新

您需要为自己创建一个 ViewModel ,因此它看起来像这样:

public class IndexViewModel
{
  public List<Movie> Movies {get; set;}
  public List<NewModel> NewModels {get; set;}
}

public ViewResult Index()
{
    var viewModel = new IndexViewModel
    {
      Movies = db.Movies.ToList(),
      NewModels = new List<NewModel>() // Or however you populate it
    };

    return View(viewModel );
}

电影将是这样的

@foreach (var moive in Model.Movies) { }

然后在您看来,您的电影和 您的新模型部分将是

@Html.Partial("_NewPartialView", Model.NewModels)

注意,您将需要更新索引页面顶部的模型以引用 IndexViewModel。

Looking at your error message your view is expecting an IEnumerable which contains classes of Project.Models.NewModel where as your passing a List containing classes of Project.Models.Movie. Basically your passing your view data it's not designed to deal with. You can either change your view/partial at the top to read something like:

@model List<Project.Models.Movie>

This sets you view to expect a list of moives which is what your passing your view from your controller in the example. On a side note

@model IEnumerable<Project.Models.Movie>

would work as well as a List inherits from IEnumerable.

If you want the view to accept an IEnumerable<Project.Models.NewModel> then you need to modify your controller to something like: (I'm making assumptions about your model properties here)

public ViewResult Index()
{
    return View(db.Movies.Select(m => new NewModel { Title = m.Title, Director = m.Director}));
}

This will then pass an IEnumerable<Project.Models.NewModel> to you view.

On another note a simpler way to reference partials in razor is:

@Html.Partial("_NewPartialView")

Update

You need to create yourself a ViewModel so it will look something like this:

public class IndexViewModel
{
  public List<Movie> Movies {get; set;}
  public List<NewModel> NewModels {get; set;}
}

public ViewResult Index()
{
    var viewModel = new IndexViewModel
    {
      Movies = db.Movies.ToList(),
      NewModels = new List<NewModel>() // Or however you populate it
    };

    return View(viewModel );
}

Then in your view it will be something like this for your movies

@foreach (var moive in Model.Movies) { }

and you new model partials will be

@Html.Partial("_NewPartialView", Model.NewModels)

Note you will need to update your model at the top of the index page to reference IndexViewModel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文