【问题标题】:How would I go about displaying data from one view in another view我将如何在另一个视图中显示来自一个视图的数据
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您可以在网格的每一行中呈现按钮/链接,而不是在外部提供按钮,如下所示

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-22
      • 2021-09-01
      • 2011-06-21
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多