【发布时间】:2017-03-21 12:33:20
【问题描述】:
抱歉标题令人困惑,我无法解释我想要实现的目标。我有一个视图,当前显示我的数据库中的“旅行”(目的地、类型、价格等)。我使用了一个操作链接,我的目标是能够选择一次旅行,单击操作链接并被带到“预订”页面。预订页面将显示所选操作链接的信息,我将添加一个下拉列表来选择旅行的人数。
实现这一目标的最佳方法是什么?会议在这里可行吗?我对 MVC 完全陌生,并且仍在尝试围绕它的工作方式进行思考!提前感谢所有阅读本文的人!
TripController 代码 -
namespace WebApplication5.Controllers
{
public class TripController : Controller
{
// GET: Trip
public ActionResult Index()
{
return View();
}
public ActionResult List()
{
List<Trips> trips = new List<Trips>();
using (SampleModel dc = new SampleModel())
{
trips = dc.Trips.ToList();
}
return View(trips);
}
public ViewResult Booking()
{
return View();
}
}
}
TripsModel 代码 -
[Table("Trips")]
public partial class Trips
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int TripID { get; set; }
[StringLength(50)]
public string Destination { get; set; }
[StringLength(50)]
public string Type { get; set; }
public int? Price { get; set; }
}
行程列表查看代码 -
@model List<WebApplication5.Models.DB.Trips>
@{
ViewBag.Title = "List of trips";
var grid = new WebGrid(source: Model, canPage: true, rowsPerPage: 10);
grid.Pager(WebGridPagerModes.All);
}
<h2>List of Trips</h2>
<div id="content">
@grid.GetHtml(
tableStyle: "webgrid-table",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
rowStyle: "webgrid-row-style",
columns: grid.Columns(
grid.Column(columnName: "TripID", header: "TripID"),
grid.Column(columnName: "Destination", header: "Destination"),
grid.Column(columnName: "Type", header: "Type"),
grid.Column(columnName: "Price", header: "Price"),
grid.Column(columnName: "Book", format: (item) => Html.ActionLink("Book", "Booking", new { id = item.TripID }))
))
</div>
TripBooking查看代码-
@model WebApplication5.Models.DB.Trips
【问题讨论】:
-
链接而不是单选按钮会使这更容易,但是使用当前的 UI,您需要处理按钮的
.click()事件并为要重定向到的方法构建一个 url (包括所选单选按钮的值,即Trip的 ID),然后使用location.href = yourUrl; -
链接如何使它更容易,我需要改变什么来用链接替换单选按钮?谢谢@StephenMuecke
-
因为它可以简单地是
@Html.ActionLink("Book", "Booking", new { id = item.TripID })(更多细节请参考this answer) -
@StephenMuecke 我仍然无法以这种方式工作,即使在阅读了您提供的答案之后也是如此。我创建了一个预订视图,为预订舱位添加了返回 View(),但预订视图没有显示在上一页中选择的行程!
-
我不是通灵者,无法猜测您的代码中有哪些错误。编辑您的问题,显示您尝试过的内容(并删除所有不相关的代码,例如您的 css - 我们只需要查看 2 个属性之一 - How to create a Minimal, Complete, and Verifiable example
标签: c# asp.net asp.net-mvc ado.net-entity-data-model