我正在制作一个电影评论网站(没有实体框架)(不打算使用它)
以下代码:我是如何根据下面的查询获取电影信息的
<div class="panel panel-default" style="background-color:white;">
<div class="panel-body">
<div style="height:400px; width:100%;">
<section class="center slider" style="width: 88%;">
@*Aqui va el poster de reviews recientes*@
@foreach (var poster in Model.LGetLastReviews)
{
<div>
<a href="/Review/Review/?=@poster.Id" id="tag<%=count++%" name="@poster.Id">
<img class="sizecool hvr-shrink" src="@Href("~/Content/"+@poster.Path )"/>
</a>
<div class="subtitulo">
@poster.Title
@poster.Date
</div>
</div>
}
</section>
</div>
</div>
</div>
以下是查询:
CREATE PROCEDURE [dbo].[GetLastReviews] @Top bigint
AS
BEGIN
SELECT TOP(@Top) Movie.Id, MOVIE.title, Poster.path_image, review.comment, review.Date from review
inner join movie on review.Movie_Id = movie.Id
inner join poster on movie.Id = poster.Movie_Id
order by review.Date desc
END
现在真正的问题是:如何根据用户点击的内容填写其他视图? 获取img id,name..etc并填写此视图:
类:
public class GetLastReviews
{
public long Id { get; set; }
public string Title { get; set; }
public string Path { get; set; }
public string Comment { get; set; }
public DateTime Date { get; set; }
}
public static List<GetLastReviews> GetLastReviews(int top)
{
List<GetLastReviews> lastReviewses= new List<GetLastReviews>();
SqlConnection connection = new SqlConnection(StrConnection);
string cmd = "GetLastReviews";
SqlCommand sqlCmd = new SqlCommand(cmd, connection);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add("@Top", SqlDbType.VarChar).Value = top.ToString();
connection.Open();
SqlDataReader dr = sqlCmd.ExecuteReader();
while (dr.Read())
{
GetLastReviews getLasR = new GetLastReviews();
getLasR.Id = long.Parse(dr["Id"].ToString());
getLasR.Title = dr["Title"].ToString();
getLasR.Path = dr["Path_Image"].ToString();
getLasR.Comment = dr["Comment"].ToString();
getLasR.Date = DateTime.Parse(dr["Date"].ToString());
lastReviewses.Add(getLasR);
}
connection.Close();
return lastReviewses;
}
答案 0 :(得分:7)
只需将div的click事件上的一个函数与uniqueId(MovieId或any)绑定在一起,如下所示。
@foreach (var poster in Model.LGetLastReviews)
{
<div onclick="GoToMovieDetail(@Model.MovieId)">
<a href="/Review/Review/?=@poster.Id" id="tag<%=count++%" name="@poster.Id">
<img class="sizecool hvr-shrink" src="@Href("~/Content/"+@poster.Path )"/>
</a>
<div class="subtitulo">
@poster.Title
@poster.Date
</div>
</div>
}
在Javascript中创建一个函数,如下所示。
function GoToMovieDetail(id)
{
if(id != null)
{
var url = "@Url.Action("DisplayMovieDetailActionName", "MovieControllerName")" + "?MovieId=" + id;
window.location.href = url;
}
}
在Controller中创建一个Action,根据请求的Id in参数获取Movie Detail,如下所示。
public ActionResult DisplayMovieDetail(int id)
{
MovieModel model = new MovieModel();
// Get the detail of moview based on requested id in action parameter.
// Bind MovieModel object.
// Pass thid object in your return View.
// In view you need to just bind the model data.
return View(model);
}