C# ASP.NET MVC-列表
我正在做一个关于电影的项目。我希望在开始页面上的列表中显示电影字幕,当您单击其中一个列表项时,它应该重定向到url中包含电影字幕的子页面(Movie.cshtml)。该页面上的信息应该是该特定电影的标题、类型、年份和国家 My StartController.cs:C# ASP.NET MVC-列表,c#,asp.net,asp.net-mvc,razor,C#,Asp.net,Asp.net Mvc,Razor,我正在做一个关于电影的项目。我希望在开始页面上的列表中显示电影字幕,当您单击其中一个列表项时,它应该重定向到url中包含电影字幕的子页面(Movie.cshtml)。该页面上的信息应该是该特定电影的标题、类型、年份和国家 My StartController.cs: using IMDB.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System
using IMDB.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace IMDB.Controllers
{
public class StartController : Controller
{
// GET: Start
public ActionResult Index()
{
var movieList = new List<Movies>();
movieList.Add(new Movies() { Title = "Terminator", Genre = "Action", Year = 1984, Country = "America" });
movieList.Add(new Movies() { Title = "Terminator II", Genre = "Action", Year = 1984, Country = "America" });
return View(movieList);
}
public ActionResult Movie(string title)
{
return View();
}
}
}
以及my Index.cshtml中的电影列表:
@model List<IMDB.Models.Movies>
@{
ViewBag.Title = "Start";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Welcome to My Movie Database</h2>
<ul>
@foreach (var movie in Model)
{
<li>
@Html.ActionLink(movie.Title, "Movie", new { title = movie.Title})
</li>
}
</ul>
@型号列表
@{
ViewBag.Title=“开始”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
欢迎来到我的电影数据库
@foreach(模型中的var电影)
{
-
@ActionLink(movie.Title,“movie”,新的{Title=movie.Title})
}
这是我的Movie.cshtml页面:
@model IMDB.Models.Movies
@{
ViewBag.Title = "Movie";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>
<h2>@Html.DisplayFor(model => model.Title)</h2>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Title)
</dt>
<dd>
@Html.DisplayFor(model => model.Title)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Genre)
</dt>
<dd>
@Html.DisplayFor(model => model.Genre)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Year)
</dt>
<dd>
@Html.DisplayFor(model => model.Year)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Country)
</dt>
<dd>
@Html.DisplayFor(model => model.Country)
</dd>
</dl>
</div>
@model IMDB.Models.Movies
@{
ViewBag.Title=“电影”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
@DisplayFor(model=>model.Title)
@DisplayNameFor(model=>model.Title)
@DisplayFor(model=>model.Title)
@DisplayNameFor(model=>model.Genre)
@DisplayFor(model=>model.Genre)
@Html.DisplayNameFor(model=>model.Year)
@DisplayFor(model=>model.Year)
@Html.DisplayNameFor(model=>model.Country)
@DisplayFor(model=>model.Country)
因此,我的问题是,如何从列表中获取唯一信息?请记住,在
Index
方法中创建的列表将在页面呈现后不再存在。客户端页面与服务器上运行的代码没有连接
您需要在Movie
方法中仅使用Movie
数据的名称来检索Movie
数据。例如,如果使用存根列表而不是DB查询,则还需要使其在该方法中可见:
public class StartController : Controller
{
List<Movie> movieList = new List<Movies>()
{
new Movies() { Title = "Terminator", Genre = "Action", Year = 1984, Country = "America" },
new Movies() { Title = "Terminator II", Genre = "Action", Year = 1984, Country = "America" }
};
// GET: Start
public ActionResult Index()
{
return View(movieList);
}
public ActionResult Movie(string title)
{
var movie = movieList.Where(x => x.Title == title).FirstOrDefault();
return View(movie);
}
}
公共类StartController:控制器
{
List movieList=新列表()
{
新电影(){Title=“Terminator”,Genre=“Action”,Year=1984,Country=“America”},
新电影(){Title=“Terminator II”,Genre=“Action”,Year=1984,Country=“America”}
};
//开始:开始
公共行动结果索引()
{
返回视图(电影列表);
}
公共ActionResult电影(字符串标题)
{
var movie=movieList.Where(x=>x.Title==Title.FirstOrDefault();
返回视图(电影);
}
}
当然,在这个场景中,您可以只使用
索引方法中的标题列表,而不是整个对象。您可以在控制器中使用修改的路由和参数创建另一个方法
您可能还应该将电影存储在某个地方,可能是存储库,而不是另一个范围中的列表
[HttpGet("movies/{title}")]
public ActionResult Movie(string title)
{
// Recreate list here... or store it somewhere and query against it in the below line
Movie movie = movieList.FirstOrDefault(x => x.Title == title);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
首先,类名称应始终为单数-电影
public class Movie
{
public int Id { get; set; }
public string Title { get; set; }
public string Genre { get; set; }
public int Year { get; set; }
public string Country { get; set; }
}
第二,在查询字符串中传递值时要使用标识符
标题可以包含特殊字符,例如:&。如果是这样,它将不是有效的URL,并且您将无法从标题中检索电影
public class StartController : Controller
{
// This could later comes from Database.
public IList<Movie> Movies
{
get
{
var movies = new List<Movie>
{
new Movie {Id = 1, Title = "Terminator", Genre = "Action", Year = 1984, Country = "America"},
new Movie {Id = 2, Title = "Terminator II", Genre = "Action", Year = 1984, Country = "America"}
};
return movies;
}
}
// GET: Start
public ActionResult Index()
{
return View(Movies);
}
public ActionResult Movie(int id)
{
var movie = Movies.FirstOrDefault(m => m.Id == id);
return View(movie);
}
}
公共类StartController:控制器
{
//这可能稍后来自数据库。
公共电影
{
得到
{
var movies=新列表
{
新电影{Id=1,Title=“Terminator”,Genre=“Action”,Year=1984,Country=“America”},
新电影{Id=2,Title=“Terminator II”,Genre=“Action”,Year=1984,Country=“America”}
};
返回电影;
}
}
//开始:开始
公共行动结果索引()
{
返回视图(电影);
}
公共行动结果电影(int id)
{
var movie=Movies.FirstOrDefault(m=>m.Id==Id);
返回视图(电影);
}
}
看法
@foreach(模型中的var电影)
{
-
@ActionLink(movie.Title,“movie”,新的{id=movie.id})
}
您是从数据库中获取数据还是使用内存中的数据结构,正如您在问题中所示?如果找不到标题,Single将抛出异常;如果(movie==null)
谢谢,它甚至不会到达,我现在已经创建了一个更好的存储库。谢天谢地,我不知道有什么奇怪的事,但我知道!我采纳了您的建议,使用Identifier代替Identifier。它解决了问题还是您仍然有与原始问题相关的问题?解决了大部分问题
public class StartController : Controller
{
// This could later comes from Database.
public IList<Movie> Movies
{
get
{
var movies = new List<Movie>
{
new Movie {Id = 1, Title = "Terminator", Genre = "Action", Year = 1984, Country = "America"},
new Movie {Id = 2, Title = "Terminator II", Genre = "Action", Year = 1984, Country = "America"}
};
return movies;
}
}
// GET: Start
public ActionResult Index()
{
return View(Movies);
}
public ActionResult Movie(int id)
{
var movie = Movies.FirstOrDefault(m => m.Id == id);
return View(movie);
}
}
<ul>
@foreach (var movie in Model)
{
<li>
@Html.ActionLink(movie.Title, "Movie", new { id = movie.Id})
</li>
}
</ul>