简体   繁体   English

如何将数据库信息传输到 asp.net mvc 中的另一个 controller

[英]How to transfer database info to another controller in asp.net mvc

My goal: user chooses an item from list (list of items from database) and this item will show on another page.我的目标:用户从列表中选择一个项目(来自数据库的项目列表),该项目将显示在另一个页面上。

I want to transfer key from database, that is chosen in one controller and have access of this chosen key from another controller.我想从数据库中传输密钥,该密钥是在一个 controller 中选择的,并且可以从另一个 controller 访问这个选择的密钥。

My database was created by Entity Framework, one of my model.我的数据库是由我的 model 之一的 Entity Framework 创建的。

My model of database:我的数据库 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
}

View, where user chooses his apartment:查看,用户选择他的公寓:

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

Hidden type is an input, where I take a value of my key.隐藏类型是一个输入,我在其中获取我的键的值。

Controller method of view: 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);
}

Controller that I need to transfer ApartmentId (key) is default:我需要传输 ApartmentId (key) 的 Controller 是默认的:

public ActionResult Confirm()
{
    return View();
}

I heard that better way to did it by cookies.我听说 cookies 有更好的方法。 But I don't know how.但我不知道怎么做。

You can either create a cookie and read the content of the cookie or you can use sessions.您可以创建 cookie 并读取 cookie 的内容,也可以使用会话。 See example: https://andrewlock.net/an-introduction-to-session-storage-in-asp-net-core/参见示例: https://andrewlock.net/an-introduction-to-session-storage-in-asp-net-core/

Or another solution is to save their selection in the database and fetch it where you need it.或者另一种解决方案是将他们的选择保存在数据库中并在需要的地方获取它。

Update: Edit the form to post the key to /confirm/ so no need to persit this value in a cookie or a session更新:编辑表单以将密钥发布到 /confirm/,因此无需在 cookie 或 session 中保留此值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM