繁体   English   中英

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

[英]How would I go about displaying data from one view in another view

对于这个令人困惑的标题感到抱歉,我无法解释我想要实现的目标。 我有一个视图,当前显示我的数据库中的'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

所以最简单的想法是编写一个类似的脚本函数

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

在控制器方面

public Task ActionResult Booking(int price)
{
//Do your stuff
}

您可以在网格的每一行中渲染按钮/链接,而不是将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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM