如何在另一个视图中显示来自一个视图的数据

时间:2017-03-21 12:33:21

标签: c# asp.net asp.net-mvc ado.net-entity-data-model

对于令人困惑的标题感到抱歉,我无法解释我想要实现的目标。我有一个观点,目前显示' Trips'来自我的数据库(目的地,类型,价格等)。我使用了一个动作链接,我的目的是能够选择一个旅行,点击动作链接并进入预订'页。预订页面将显示所选动作链接的信息,我将添加一个下拉列表以选择旅行中的人数。

实现这一目标的最佳方法是什么?会议是否可行?我对MVC来说是全新的,我仍然试图绕着它的工作方式前进!提前感谢任何阅读此内容的人!

actionLink

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; }
}

Trip ListView代码 -

@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>

TripBookingView代码 -

@model WebApplication5.Models.DB.Trips

2 个答案:

答案 0 :(得分:0)

所以最简单的想法就是编写像

这样的脚本函数
Inspect.exe

并在控制器方面

 @section scripts{
<script>
$(function(){
$('#book').on('click',function(){
 windows.location.href('@url.ActionLink('Booking','Trip',new{price = price}))
})
})
<script>
}

答案 1 :(得分:0)

您可以在网格的每一行中呈现按钮/链接,而不是将Button放在外面

@grid.GetHtml(columns: new [] {
grid.Column(
"",
header: "Actions",
format: @<text>@Html.ActionLink("Book", "Book", new { id=item.TripID})</text> ) });

并在控制器中将控制器更改为

public ActionResult Book(int TripID)
{
  //bring required data from DB for the TripID you received
}