使用会话将数据从视图传递到其他视图

时间:2015-09-07 10:00:55

标签: c# asp.net-mvc-4 session

更改了我的帖子描述,因为我已经阅读过" sessions"是正确的使用方式。问题是我发现使用会话很困难,因为我之前从未使用过它。

我试图将我的文本框与模型链接并将这些值传递给另一个在另一个控制器上运行的视图。我想要的只是显示您在SeatController索引页面中输入的值,以便稍后我可以将它们用于我的预订项目。

到目前为止我得到了什么:

  • 包含索引视图的BookingController
  • 包含其他索引视图的SeatController

BookingController:

public class BookingController : Controller
{

    // GET: Booking
    [HttpGet]
    public ActionResult Index()
    {
        var vm = new BookingModel();            
        return View(vm);
    }

    [HttpPost]
    public ActionResult Index(BookingModel bookingModel)
    {

        if (ModelState.IsValid)
        {
            Session["SelectedValue"] = bookingModel;
            return RedirectToAction("Index", "Seat", new { model = bookingModel });
        }
        return View(bookingModel);
    }       
}

BookingModel:

 public class BookingModel
{
    public string DepartureRoute { get; set; }
    public string ReturnRoute { get; set; }
    public DateTime DepartureDate { get; set; }
    public DateTime ReturnDate { get; set;}
    public int adults { get; set; }
    public int childrens {get; set; }
}

索引(BookingController)

@model TestApplication.Models.BookingModel
@Session["SelectedValue"]


<div class="col-sm-9">

@using (Html.BeginForm())
{
    <div class="col-sm-6">
        <label>Outward route</label><br />
        @Html.TextBoxFor(m => Model.DepartureRoute, new { @class = "textbox" })

        <br />

        <label>Departure date</label><br />
        @Html.TextBoxFor(m => m.DepartureDate, new { @class = "textbox" })

        <br />

        <label>Adults:</label><br />
        @Html.TextBoxFor(m => Model.DepartureRoute, new { @class = "textbox" })
    </div>

    <div class="col-sm-6">

        <label>Return route</label><br />
        @Html.TextBoxFor(m => Model.DepartureRoute, new { @class = "textbox" })

        <br />

        <label>Return date</label><br />
        @Html.TextBoxFor(m => m.ReturnDate, new { @class = "textbox" })

        <br />

        <label>Childrens:</label><br />
        @Html.TextBoxFor(m => Model.DepartureRoute, new { @class = "textbox" })

        <br />
        <label>Information:</label><br />
        <button type="submit" class="button_continue">Continue</button> 
    </div>
}
</div>

&#34; SeatController&#34;上的索引页面看起来像这样:

索引(SeatController)

<div class="col-sm-12">
<label>Departure route</label><br />
@Html.DisplayFor(model => model.DepartureRoute)

<br />

<label>Return route</label><br />
@Html.DisplayFor(m => m.ReturnRoute);

<br />

<label>Departure date</label><br />
@Html.DisplayFor(m => m.DepartureDate);

<br />

<label>Return date</label><br />
@Html.DisplayFor(m => m.ReturnDate);

<br />

<label>Number of adults</label><br />
@Html.DisplayFor(m => m.adults);

<br />

<label>Number of childrens</label><br />
@Html.DisplayFor(m => m.childrens);
</div>

希望有人可以提供解决方案或指导如何执行此操作。

1 个答案:

答案 0 :(得分:0)

您可以使用TempData来解决问题。

  

ASP.NET MVC TempData字典用于在之间共享数据   控制器动作。阅读更多here ....

[HttpPost]
public ActionResult Index(BookingModel bookingModel)
{
  if (ModelState.IsValid)
  {
     TempData["BM"] = bookingModel;
     return RedirectToAction("Result");
  }
  return View(bookingModel);
}


public ActionResult Result()
{
   if (TempData["BM"] != null)
   {
     BookingModel bookingModel = (BookingModel) TempData["BM"];
     return View(bookingModel);
   }

}