【发布时间】:2016-04-19 21:12:26
【问题描述】:
我的问题是,当单击同一页面上的详细信息链接时,如何显示所选电影的详细信息?当前,当您单击超链接时,您将被重定向到将显示有关该电影的信息的控制器和操作,例如Movies/Details/1。我希望它显示在Movies
我使用位于此处的示例代码:http://code.msdn.microsoft.com/MVC5-Demo-with-Entity-c6bc81df 创建了我的 MVC 应用程序。
它使用 CRUD 创建读取、更新和删除记录。
这样做的原因是我正在创建一个与按钮一起使用的网站,以在数据库中显示适当的数据,例如 Button1 - ID1、Button2 - ID2 等......
这可以在列出电影的同一页面上详细说明吗?
Index.cshtml
@model IEnumerable<MvcMovie.Models.Movie>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>@Html.DisplayNameFor(model => model.Title)</th>
<th>@Html.DisplayNameFor(model => model.Genre)</th>
<th>@Html.DisplayNameFor(model => model.Price)</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(modelItem => item.Title)</td>
<td>@Html.DisplayFor(modelItem => item.Genre)</td>
<td>@Html.DisplayFor(modelItem => item.Price)</td>
<td>
<a href='@Url.Action("Details", new { id = item.ID })' class="ajax">Details</a>
</td>
</tr>
}
</table>
<div id="TargetDiv"></div>
<script src="../../Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".ajax").on("click", function (e) {
e.preventDefault();
var elementUrl = $(this).attr('href');
$.ajax({
url: elementUrl,
cache: false,
success: function (data) {
$('#TargetDiv').html(data);
}
});
});
});
</script>
电影控制器
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMovie.Models;
namespace MvcMovie.Controllers
{
public class Movies2Controller : Controller
{
private Jess_MoviesEntities db = new Jess_MoviesEntities();
public ActionResult Index()
{return View(db.Movies.ToList());}
public ActionResult OneMovie(int id)
{return View();}
}
}
【问题讨论】:
-
你能告诉我们你的看法吗
-
@DonaldJansen 我已经在上面的视图代码中添加了。需要任何其他代码让我知道。
-
我添加了一个可能有帮助的答案
标签: c# asp.net asp.net-mvc-4 button partial-views