Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.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# 在这种情况下,如何从索引向视图传递信息-MVC3_C#_Asp.net Mvc 3 - Fatal编程技术网

C# 在这种情况下,如何从索引向视图传递信息-MVC3

C# 在这种情况下,如何从索引向视图传递信息-MVC3,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我编写了以下代码: public ActionResult Index() { var folders = Directory.GetDirectories(Server.MapPath("~/Content/themes/base/songs")); foreach (var folder in folders) {

我编写了以下代码:

public ActionResult Index()
        {                            
            var folders = Directory.GetDirectories(Server.MapPath("~/Content/themes/base/songs"));

            foreach (var folder in folders)
            {
                var movieName = new DirectoryInfo(folder).Name;
                string[] files = Directory.GetFiles(folder);
                string img = string.Empty;
                List<string> song = new List<string>();
                foreach (var file in files)
                {
                    if (Path.GetExtension(file) == ".jpg" ||
                        Path.GetExtension(file) == ".png")
                    {
                        img = Path.Combine(Server.MapPath("~/Content/themes/base/songs"), file);
                    }
                    else 
                    {
                        song.Add(Path.Combine(Server.MapPath("~/Content/themes/base/songs"), file));
                    }
                }
            }
            return View();
        }
public ActionResult Index()
{                            
var folders=Directory.GetDirectories(Server.MapPath(“~/Content/themes/base/songs”);
foreach(文件夹中的var文件夹)
{
var movieName=新目录信息(文件夹).Name;
string[]files=目录.GetFiles(文件夹);
string img=string.Empty;
列表歌曲=新列表();
foreach(文件中的var文件)
{
if(Path.GetExtension(file)=“.jpg”||
Path.GetExtension(文件)=“.png”)
{
img=Path.Combine(Server.MapPath(“~/Content/themes/base/songs”),文件);
}
其他的
{
添加(Path.Combine(Server.MapPath(“~/Content/themes/base/songs”),文件));
}
}
}
返回视图();
}

我想做的是用电影图片传递20个电影名称,每部电影都有大约4或5首歌曲,应该显示在下面。我已经知道了如何捕获上面的所有信息,但我不知道如何将其传递到视图中显示。有人能帮我吗?

你应该填充一个模型对象。。。并将其通过回油管:

var theModel = new MyModel();
...
//All the loading model info

return View(theModel)
在您的视图中,需要在顶部设置一条线,如下所示:

@model YourProject.MyModel

然后,通过
@Model
对象进行循环。

我想您应该在应用程序中添加一些类。例如,Movie和MovieSong以及您的电影类应该具有类似IList图像的内容。然后,您可以轻松地将电影传送到您的视图

我不确定此代码是否有效,但您可以尝试以下方法:

public ActionResult Index()
{   
    var movies = new List<Movie>();

    var songsPath = Server.MapPath("~/Content/themes/base/songs");
    var folders = Directory.GetDirectories(songsPath);

    foreach (var folder in folders)
    {
        Movie movie = new Movie();
        movie.MovieName = new DirectoryInfo(folder).Name

        string[] files = Directory.GetFiles(folder);

        foreach (var file in files)
        {
            if (Path.GetExtension(file) == ".jpg" ||
                Path.GetExtension(file) == ".png")
            {
                movie.Images.Add(Path.Combine(songsPath, file));
            }
            else 
            {
                movie.Songs.Add(Path.Combine(songsPath, file));
            }
        }

        movies.add(movie);
    }
    return View(movies);
}
public ActionResult Index()
{   
var movies=新列表();
var songsPath=Server.MapPath(“~/Content/themes/base/songs”);
var folders=Directory.GetDirectories(songsPath);
foreach(文件夹中的var文件夹)
{
电影=新电影();
movie.MovieName=新建目录信息(文件夹).Name
string[]files=目录.GetFiles(文件夹);
foreach(文件中的var文件)
{
if(Path.GetExtension(file)=“.jpg”||
Path.GetExtension(文件)=“.png”)
{
movie.Images.Add(Path.Combine(songsPath,file));
}
其他的
{
movie.Songs.Add(Path.Combine(songsPath,file));
}
}
添加(电影);
}
返回视图(电影);
}

Q1。我不知道如何将其传递到视图中以显示

答:您需要使用视图模型,下面是我为此准备的视图模型

public class Movie
{
    public string Name;
    public string ImagePath;
    ....
    ....
    //Add more as per your requirement
}
将您拥有的所有数据推送到该模型中

Q2。我想做的是用电影图像传递20个电影名称,每部电影都有大约4或5首歌曲,应该显示在下面

答:现在,由于您拥有的是一组电影,您需要将此电影类的列表传递给模型

public ActionResult Index()
{   
    var movies = new List<Movie>();

    // populate the data

    return View(movies);
}
public ActionResult Index()
{   
var movies=新列表();
//填充数据
返回视图(电影);
}
在视图中显示它

@model ProjectName.Models.List<Movies>

@foreach(var item in Model)
{
    <h1>Movie Name : </h1> @item.Name
    ....
    .... //etc etc
}   
@model ProjectName.Models.List
@foreach(模型中的var项目)
{
电影名称:@item.Name
....
…等等
}   
希望这有帮助