![](/img/trans.png)
[英]Transfer parameter from one controller to another in ASP.NET MVC
[英]How to transfer database info to another controller in asp.net mvc
我的目標:用戶從列表中選擇一個項目(來自數據庫的項目列表),該項目將顯示在另一個頁面上。
我想從數據庫中傳輸密鑰,該密鑰是在一個 controller 中選擇的,並且可以從另一個 controller 訪問這個選擇的密鑰。
我的數據庫是由我的 model 之一的 Entity Framework 創建的。
我的數據庫 model:
public class Apartment
{
// id of apartment
[HiddenInput(DisplayValue = false)]
public int ApartmentId { get; set; }
// quantity in apartment
[Required(ErrorMessage = "Поле должно быть установлено")]
public int Quantity { get; set; }
// class of apartment
[Required(ErrorMessage = "Поле должно быть установлено")]
public Classes Class { get; set; }
// price of apartment
[Required(ErrorMessage = "Поле должно быть установлено")]
public int Price { get; set; }
// start of book apartment
[Required(ErrorMessage = "Поле должно быть установлено")]
public DateTime Start { get; set; }
// end of book apartment
public DateTime End { get; set; }
}
// Class apartment in enum
public enum Classes
{
Economy,
Standard,
Luxury
}
查看,用戶選擇他的公寓:
foreach (Hotel.DAL.Entities.Apartment a in Model)
{
<div id="ap" class="col-md-3">
<p>Class: @a.Class
<p>Quantity: @a.Quantity человек
<p>Price for one day: @a.Price $
@if (ViewBag.TimeInt != null)
{
ViewBag.FullPrice = a.Price * ViewBag.TimeInt;
<p>Full price: @ViewBag.FullPrice $</p>
}
@if (HttpContext.Current.Request.HttpMethod == "POST")
{
<form asp-action="Index" method="post">
<p>
<button value="Open Window" onclick="window.open('/Home/Confirm')" class="btn btn-primary">Book</button>
<input type="hidden" name="id" value="@a.ApartmentId" />
</p>
</form>
}
</div>
}
隱藏類型是一個輸入,我在其中獲取我的鍵的值。
Controller方法查看:
public ActionResult Index(string sortOrder)
{
ViewData["ClassSortParm"] = sortOrder == "Class" ? "class_desc" : "Class";
ViewData["PriceSortParm"] = sortOrder == "Price" ? "price_desc" : "Price";
var p = from s in dbshow.GetList()
select s;
switch (sortOrder)
{
case "class_desc":
p = dbshow.GetList().OrderByDescending(s => s.Class);
break;
case "price_desc":
p = dbshow.GetList().OrderByDescending(s => s.Price);
break;
default:
p = dbshow.GetList().OrderBy(s => s.Class);
break;
}
return View(p);
}
// filters to book apartments
[HttpPost]
public ActionResult Index(string sortOrder, Classes? Class, int? quantity, DateTime? start, DateTime? end, bool? check)
{
ViewData["CurrentQuan"] = quantity;
ViewData["CurrentClass"] = Class;
ViewData["CurrentStart"] = start;
ViewData["CurrentEnd"] = end;
ViewData["CurrentCheck"] = check.GetValueOrDefault();
ViewData["ClassSortParm"] = sortOrder == "Class" ? "class_desc" : "Class";
ViewData["PriceSortParm"] = sortOrder == "Price" ? "price_desc" : "Price";
var aps = from s in dbshow.GetList()
select s;
return View(aps);
}
我需要傳輸 ApartmentId (key) 的 Controller 是默認的:
public ActionResult Confirm()
{
return View();
}
我聽說 cookies 有更好的方法。 但我不知道怎么做。
您可以創建 cookie 並讀取 cookie 的內容,也可以使用會話。 參見示例: https://andrewlock.net/an-introduction-to-session-storage-in-asp-net-core/
或者另一種解決方案是將他們的選擇保存在數據庫中並在需要的地方獲取它。
更新:編輯表單以將密鑰發布到 /confirm/,因此無需在 cookie 或 session 中保留此值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.