如何在ASP.NET MVC中显示从另一个控制器到另一个控制器的ID项目

时间:2018-12-12 07:44:17

标签: c# jquery controller parameter-passing

我已经将一个ID从房间传递给了预订控制器

 <button type="button" onclick="location.href='@Url.Action("Create", "Bookings", new { id = item.RoomID })'" class="btn btn-info">Book</button>

现在我想在单击“预订”按钮后向其他控制器显示该ID,这是图像 i want to display this item

现在,我要粘贴到另一个将ID显示为文本的控件,并在此处显示,但是图像未显示细节。 enter image description here

这是我在预订控制器中的代码,

 public ActionResult Create(int id)
    {
        var bookings = db.Bookings.Include(b => b.Room).Include(b => b.Register);



        ViewBag.RoomID = new SelectList(db.Rooms, "RoomID", "RoomType");
        ViewBag.CustomerID = new SelectList(db.Registers, "id", "username");
        return View();
    }

我应该怎么做才能在预订控制器中显示房间ID的详细信息?

这是查看代码, enter image description here

2 个答案:

答案 0 :(得分:1)

您的代码中可能存在一些问题:

1)避免将viewmodel属性名称用作ViewBag属性名称,因为这可能会导致它们之间的混淆。

2)您不会通过将第二个参数设置为空值来填充DropDownList帮助器中的任何内容,而使用包含ViewBagSelectList的{​​{1}}属性来填充它。 / p>

3)为每个视图模型属性使用强类型的List<SelectListItem>

基于以上几点,控制器操作应如下所示:

DropDownListFor

两个下拉列表都应使用强类型的帮助器,如下例所示:

public ActionResult Create(int id)
{
    var bookings = db.Bookings.Include(b => b.Room).Include(b => b.Register);

    var model = new ViewModel();

    // use query to get both selected IDs
    model.RoomId = bookings.Where(...).Select(x => x.RoomId).FirstOrDefault();
    model.CustomerId = bookings.Where(...).Select(x => x.CustomerId).FirstOrDefault();

    ViewBag.RoomList = new SelectList(db.Rooms, "RoomID", "RoomType");
    ViewBag.CustomerList = new SelectList(db.Registers, "id", "username");
    return View(model);
}

注意:最好在类型为@Html.DropDownListFor(model => model.RoomId, ViewBag.RoomList as SelectList, "-- Select Room --", new { @class = "form-control" }) @Html.DropDownListFor(model => model.CustomerId, ViewBag.CustomerList as SelectList, "-- Select Customer --", new { @class = "form-control" }) / SelectList的viewmodel属性中填充选项列表,并将其直接传递给视图:

模型

List<SelectListItem>

控制器操作

public List<SelectListItem> RoomList { get; set; }

public List<SelectListItem> CustomerList { get; set; }

查看

public ActionResult Create(int id)
{
    var bookings = db.Bookings.Include(b => b.Room).Include(b => b.Register);

    var model = new ViewModel();

    // use query to get both selected IDs
    model.RoomId = bookings.Where(...).Select(x => x.RoomId).FirstOrDefault();
    model.CustomerId = bookings.Where(...).Select(x => x.CustomerId).FirstOrDefault();

    model.RoomList = db.Rooms.Select(x => new SelectListItem { Text = x.RoomType, Value = x.RoomID }).ToList();
    model.CustomerList = db.Registers.Select(x => new SelectListItem { Text = x.username, Value = x.id }).ToList();
    return View(model);
}

答案 1 :(得分:0)

您可以从控制器设置选定的值。

    public ActionResult Create(int id)
    {
        var bookings = db.Bookings.Include(b => b.Room).Include(b => b.Register);

        ViewBag.RoomID = new SelectList(db.Rooms, "RoomID", "RoomType", id);
        ViewBag.CustomerID = new SelectList(db.Registers, "id", "username");
        return View();
    }