1

Have changed my post description because I have read that "sessions" is the right way to use. The problem is that I find it difficult using sessions, because I have never used it before.

I'm trying to link my Textboxes with an model and passing these values you enter to another view which operate on another controller. All I want is to show the values you enter in the SeatController Index page, so I then later on can use them for my booking project.

What I've got so far:

  • An BookingController containing an Index view
  • An SeatController containing another Index view

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

Index (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>

The Index page on the "SeatController" look like this:

Index (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>

Hope someone can give an solution or some guidelines how to do this.

Mikkel
  • 1,771
  • 11
  • 35
  • 59
  • 1
    `return RedirectToAction("Result");` does not pass the model to the `Result` method. In the `Index()` POST method, save the model to a repository and pass its ID (you don't appear to have one yet) to the `Result(int ID)` method - `return RedirectToAction("Index", new { ID = bookingModel.ID });` In the `Result()` method, get the saved model and pass it to the `Result.cshtml` view. –  Sep 07 '15 at 10:19
  • okay.. I'm trying to read something about repository. – Mikkel Sep 07 '15 at 11:15

1 Answers1

0

You can use TempData to solve your problem.

ASP.NET MVC TempData dictionary is used to share data between controller actions. Read more 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);
   }

}
Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47
  • And as soon as a user refreshes the browser, it all fails:) –  Sep 07 '15 at 10:44
  • So this is a bad solution? – Mikkel Sep 07 '15 at 10:56
  • @Mikkel you can pass something which you can uniquely identify in `Result` action like `return RedirectToAction("Index", new { somevalue = xx });` [check this](http://stackoverflow.com/questions/2269376/passing-information-between-controllers-in-asp-net-mvc) – Amnesh Goel Sep 07 '15 at 11:05