It is necessary to transfer the entered data between representations. I write them into the session and pass to another view, but at the output I get only the data type (Models.Order). But at the same time in the database data is recorded.

[HttpPost] public ActionResult Index(Order order) { Session["ord"] = order; return RedirectToAction("OrderConfirm"); } public ActionResult OrderConfirm(Order order) { ViewBag.Message = Session["ord"]; return View(); } [HttpPost] public ActionResult OrderConfirm(Order order, string action) { var orders = Session["ord"]; if (action == "Добавить") { db.Entry(orders).State = EntityState.Added; db.SaveChanges(); return RedirectToAction("Index"); } return View(); } @using (Html.BeginForm()) { <p>Подробности заказа: </p> @ViewBag.Message <br /> <input type="submit" name="action" value="Добавить" /> <input type="submit" name="action" value="Отмена" /> } 

UPD. Made with sessions

  [HttpPost] public ActionResult Index(string name, int amount, decimal sum) { Order order = new Order(); order.Name = name; order.Amount = amount; order.Sum = sum; Session["ord"] = order; return RedirectToAction("OrderConfirm"); } public ActionResult OrderConfirm() { Order order = Session["ord"] as Order; if (order != null) { ViewBag.Name = order.Name; ViewBag.Amount = order.Amount; ViewBag.Sum = order.Sum; } return View(); } [HttpPost] public ActionResult OrderConfirm(Order order, string action) { var orders = Session["ord"]; if (action == "Добавить") { db.Entry(orders).State = EntityState.Added; db.SaveChanges(); return RedirectToAction("Index"); } return View(); } 
  • Do you need to transfer data between views within the processing of a single request, or do you need to transfer data between different requests? - Andrew B

2 answers 2

Why through the session, then?

 [HttpPost] public ActionResult Index(Order order) { var myOrder = order; return RedirectToAction("OrderConfirm", new { order = myOrder}); } 

There are suspicions that you choose OrderConfirm(Order) , and not OrderConfirm(Order, string) . Try re

Try adding via db.Set<Order>().Add(order)

 [HttpPost] public ActionResult Index(Order order) { var myorder = order; return RedirectToAction("OrderConfirm", new{ order = myorder, userAction = "Добавить" }); } public ActionResult OrderConfirm(Order order) { ViewBag.Message = order; return View(); } [HttpPost] public ActionResult OrderConfirm(Order order, string userAction) { if (userAction == "Добавить") { db.Set<Order>().Add(order); db.SaveChanges(); return RedirectToAction("Index"); } return View(); } 

.

 @using (Html.BeginForm("Index", "MyCoolController", FormMethod.Post)) { <p>Подробности заказа: </p> @ViewBag.Message <br /> <input type="submit" name="userAction" value="Добавить" /> <input type="submit" name="userAction" value="Отмена" /> } 
  • 1. Nothing is written to the ViewBag.Message 2. db.Set <Order> (). Add (order); undefined value - Akmal
  • @Akmal I can not guarantee that I understand your idea 100%. Are you sure (debugger) that control is transferred to those actions that you need? - free_ze
  • I was debugging, and the only thing that is not clear is that in the line var orders = Session ["ord"]; he records my entered data, and in the ViewBag.Message = Session ["ord"] line only the Models.Order type and in the view displays it - Akmal

You can transfer data between views in the following ways:

  • Viewbag
  • Viewdata
  • Presentation model

Suppose there is a controller with this method:

 public ActionResult Index() { // SOME CODE HERE return View(); } 

To transfer data to a view, you can use one of the following options:

 ViewBag.MyProperty = 10; // Какое либо значение (строка, число или что угодно) ViewData["MyProperty"] = 10; // Какое либо значение (строка, число или что угодно) var model = new MyModel { MyProperty = 10 } // Создаем класс модели и эту модель в представление через return View(model) 

In the view, read the value:

 @ViewBag.MyProperty @ViewData["MyProperty"] @Model.MyProperty 

If you have a partial view (the view uses a different view), then the ViewBag and ViewData will be available in a partial view too. The model (or part of it) will need to be transferred to a partial view.

 @Html.Partial("MyPartialView", Model) 

In the main view, you can also fill in the ViewBag and ViewData values ​​and use them in partial ones. From a partial view to the main transfer in this way will not work.

However, I believe that you are trying to pass values ​​between different requests. Such a conclusion suggests itself from your code, since you read "ord" in various actions (in Index you fill it, and in the others you use it)

To transfer the value between requests, you can use it (stored on the server side and do not work when cookies are disabled, except the last one):

  • ApplicationCache application cache (for example, if you need to display the same value for different users)
  • Session session (if you want to save data between different requests of the same user)
  • TempData (the same as the session, but automatically cleared after the next request, or after the first use, depending on the version of asp.net)
  • Cookies (adding cookies with some name and value to the response is stored on the user's browser side )

Write in the commentary what to add to the answer in order to make it more accurate, since it will not be possible to describe all the above options with examples briefly.

  • It turns out I need to transfer between different requests. I would be grateful if you show an example with the sessions - Akmal